将Tensorflow 1.0的代码转换为2.0版本。

huangapple go评论91阅读模式
英文:

Convert Tensorflow 1.0 code into 2.0 version

问题

I am facing issue while converting my tensorflow 1.0 code into 2.0

我在将我的TensorFlow 1.0代码转换为2.0时遇到了问题

I can convert successful this

我可以成功地转换这个

Version 1

版本1

reset underlying graph data

#tf.reset_default_graph()

重置底层图数据
#tf.reset_default_graph()

Version 2

版本2

from tensorflow.python.framework import ops
ops.reset_default_graph()

从tensorflow.python.framework导入ops
ops.reset_default_graph()

This is the code below which i am having issue please help me out how can i build NN in tensorflow 2 version

以下是我遇到问题的代码,请帮助我了解如何在tensorflow 2版本中构建神经网络

Build neural network

net = tflearn.input_data(shape=[None, len(train_x[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')
net = tflearn.regression(net)

Define model and setup tensorboard

model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')

Start training (apply gradient descent algorithm)

model.fit(train_x, train_y, n_epoch=100, batch_size=8, show_metric=True)
model.save('my_drive/AI_values/model/model.ckpt')

英文:

I am facing issue while converting my tensorflow 1.0 code into 2.0

I can convert successful this

version 1

 # reset underlying graph data
 #tf.reset_default_graph()

Version 2

from tensorflow.python.framework import ops
ops.reset_default_graph()

This is the code below which i am having issue please help me out how can i build NN in tensorflow 2 version

# Build neural network
net = tflearn.input_data(shape=[None, len(train_x[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')
net = tflearn.regression(net)
# Define model and setup tensorboard
model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')
# Start training (apply gradient descent algorithm)
model.fit(train_x, train_y, n_epoch=100, batch_size=8, show_metric=True)
model.save('my_drive/AI_values/model/model.ckpt')

答案1

得分: 2

tf_upgrade_v2 — infile foo.py — outfile foo-upgraded.py

此命令可以轻松将您的TensorFlow代码从1.0升级到2.0,详细内容请参考(https://www.youtube.com/watch?v=JmSNUeBG-PQ)

英文:

tf_upgrade_v2 — infile foo.py — outfile foo-upgraded.py

this command can easily convert your tensorflow code from 1.0 to 2.0 for full thing you can refer (https://www.youtube.com/watch?v=JmSNUeBG-PQ)

答案2

得分: -2

我无法正确理解你的问题(即什么是版本1和版本2,以及你想将什么从tf 1.0转换为tf 2.0),但将任何代码从tensorflow版本1.0转换为2.0都非常容易。

在导入tensorflow时,不要使用 import tensorflow as tf,而是使用 import tensorflow.compat.v1 as tf,如果你想从tensorflow导入特定模块(例如 from tensorflow.keras.models import Model),请使用 from tensorflow.compat.v1.keras.models import Model

英文:

I could not understand your question properly (i.e what is version 1 and version 2 and what do you want to convert from tf 1.0 to tf 2.0) but to convert any code from tensorflow version 1.0 to 2.0 is very easy.

While importing tensorflow instead of doing import tensorflow as tf do import tensorflow.compat.v1 as tf and if you want to import a specific module from tensorflow(e.g from tensorflow.keras.models import Model) do from tensorflow.compat.v1.keras.models import Model.

huangapple
  • 本文由 发表于 2020年1月6日 16:43:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定