【问题标题】:Converting short tensorflow 1.13 script into tensorflow 2.0将短 tensorflow 1.13 脚本转换为 tensorflow 2.0
【发布时间】:2019-05-09 20:46:19
【问题描述】:

我正在尝试通过将我的 tensorflow1.13 脚本(如下)转换为 tensorflow2.0 脚本来了解 tensorflow2.0 的动态。但是我正在努力做到这一点。

我认为我挣扎的主要原因是因为 tensorflow2.0 的例子我见过训练神经网络,所以他们有一个model,他们有compilefit。但是,在下面的简单示例中,我没有使用神经网络,因此我看不到如何将此代码调整为 tensorflow2.0(例如,如何替换会话?)。非常感谢您的帮助,并在此先感谢您。

data = tf.placeholder(tf.int32)
theta = tf.Variable(np.zeros(100))
p_s = tf.nn.softmax(theta)

loss = tf.reduce_mean(-tf.log(tf.gather(p_s, data)))
train_step = tf.train.AdamOptimizer().minimize(loss)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(10):
        for datum in sample_data(): #sample_data() is a list of integer datapoints
            _ = sess.run([train_step], feed_dict={data:datum})
    print(sess.run(p_s))

我查看了this(这是最相关的),到目前为止我想出了以下内容:

#data = tf.placeholder(tf.int32)
theta = tf.Variable(np.zeros(100))
p_s = tf.nn.softmax(theta)

loss = tf.reduce_mean(-tf.math.log(tf.gather(p_s, **data**)))
optimizer = tf.keras.optimizers.Adam()

for epoch in range(10):
    for datum in sample_data(): 
        optimizer.apply_gradients(loss)

print(p_s)

但是上面显然没有运行,因为损失函数中的占位符 data 不再存在 - 但是我不知道如何替换它。 :S

有人吗? 请注意,我没有 def forward(x) 因为我的输入 datum 没有转换 - 它直接用于计算损失。 em>

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:

    而不是使用转换工具(存在,但我不喜欢它,因为它只是使用 tf.compat.v1 为 API 调用添加前缀(或多或少)并使用旧的 Tensoflow 1.x API)我帮你转换你的代码到新版本。

    会话消失了,占位符也消失了。原因?代码是逐行执行的——即Tensorflow Eager模式。

    要正确训练模型,您必须使用优化器。如果你想使用 minimize 方法,在 Tensorflowe 2.0 中你必须定义函数来最小化(损失)作为 Python 可调用对象。

    # This is your "model"
    theta = tf.Variable(np.zeros(100))
    p_s = tf.nn.softmax(theta)
    
    # Define the optimizer
    optimizer = tf.keras.optimizers.Adam()
    
    # Define the training loop with the loss inside (because we use the
    # .minimnize method that requires a callable with no arguments)
    
    trainable_variables = [theta]
    
    for epoch in range(10):
        for datum in sample_data():
            # The loss must be callable and return the value to minimize
            def loss_fn():
                loss = tf.reduce_mean(-tf.math.log(tf.gather(p_s, datum)))
                return loss
            optimizer.minimize(loss_fn, var_list=trainable_variables)
        tf.print("epoch ", epoch, " finished. ps: ", p_s)
    

    免责声明:我尚未测试代码 - 但它应该可以工作(或者至少让您了解如何在 TF 2 中实现您想要实现的目标)

    【讨论】:

    • 哇。谢谢。我一直离开我的电脑,直到今天晚些时候我将执行代码并报告它是否有效。再次感谢 +1
    • 我很想知道(1)损失函数上是否需要@tf.function装饰器(2)是否需要tf.gradienttape?
    • tf.function 永远不需要 - 将函数转换为其图形表示不是强制性的,仅在急切缓慢时推荐。如果你想在这列火车的某个地方添加它,只需装饰整个训练脚本(因此,把它放在一个函数中并装饰它)并使用图表执行整个训练。 (2): 当你不想使用minimize 方法时需要梯度胶带,因此当你有一个复杂的损失函数时,当你想要更大的灵活性时,当你想在应用更新规则之前处理梯度时选择的优化器。
    • 嗨@nessuno,我在运行代码时遇到错误:ValueError: No gradients provided for any variable: ['Variable:0']. 出于某种原因,我猜它不理解 theta 是一个可训练的变量
    • 尝试用tf.nn.softmax(theta)替换损失函数中的p_s,看看是否有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多