Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 为什么在model.fit期间调用两次_Tensorflow - Fatal编程技术网

Tensorflow 为什么在model.fit期间调用两次

Tensorflow 为什么在model.fit期间调用两次,tensorflow,Tensorflow,当我运行上面的代码时,会打印出2个hi。在本例中,只有1个历元,并且在该历元中有1个批次(批次大小为20),那么为什么调用方法会被调用两次。相关: 插入到调用函数中的Pythonprint将仅在第一次执行时在下面构建图形时执行。第二个调用由TF eager execution触发,这也是第一次(如果在model.fit之前运行TF.compat.v1.disable_eager_execution(),您将只看到一个打印的hi) 但是如果第二次运行model.fit(第三次,…),您将注意到没有

当我运行上面的代码时,会打印出2个hi。在本例中,只有1个历元,并且在该历元中有1个批次(批次大小为20),那么为什么调用方法会被调用两次。

相关:

插入到调用函数中的Python
print
将仅在第一次执行时在下面构建图形时执行。第二个调用由TF eager execution触发,这也是第一次(如果在
model.fit
之前运行
TF.compat.v1.disable_eager_execution()
,您将只看到一个打印的
hi


但是如果第二次运行
model.fit
(第三次,…),您将注意到没有打印任何内容。这是因为,一旦建立了图形,向前传递就不再执行
调用
函数。如果要打印与每次向前传递的执行相关的内容,应改用
tf.print(“hi”)
。您会注意到,通过这种方式,在
model.fit

中,每个eopch都会发生一次且只有一次打印,因此,tf.print('hi')会在每个model.fit调用中继续打印hi,因为tf.print会成为图形本身的一部分。我的理解正确吗?没错!tf.print成为图中的一个节点,在TF2中,它每次都会运行
import tensorflow as tf
import numpy as np
x=np.random.rand(20,10,64)
y=np.random.randint(10,size=(20,1))
class mymodel(tf.keras.Model):
  def __init__(self):
    super(mymodel,self).__init__()
    self.l1 = tf.keras.layers.LSTM(10,return_state=True)
    self.l2 = tf.keras.layers.Dense(10,activation=tf.keras.activations.softmax)
  def call(self,input):
    print('hi')
    x=self.l1(input)
    # tf.print(x[0],x[1],x[2])
    x=self.l2(x[0])
    return x
model =mymodel()
model.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy())
model.fit(x,y)