我在遍历documentation中的VAE示例之后遇到了这个问题,无法在拟合数据之前对其进行构建。
显然,它会引发以下错误:
ValueError: Expected a symbolic Tensors or a callable for the loss value. Please wrap your loss computation in a zero argument `lambda`.
这是一个简单的MWE:
import tensorflow as tf
from tensorflow.keras import layers
class Test(tf.keras.Model):
def __init__(self, input_dim=784, name='test', **kwargs):
super(Test, self).__init__(name=name, **kwargs)
self.dense1 = layers.Dense(input_dim)
self.dense2 = layers.Dense(input_dim, activation='relu')
def call(self, inputs):
y1 = self.dense1(inputs)
y2 = self.dense2(inputs)
loss = tf.keras.backend.mean(y2)
self.add_loss(loss)
return y1
(x_train, _), _ = tf.keras.datasets.mnist.load_data()
x_train = x_train.reshape(60000, 784).astype('float32') / 255
train_dataset = tf.data.Dataset.from_tensor_slices(x_train)
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(64)
test = Test()
test.compile(optimizer='adam', loss='mse')
# test.build(input_shape=(64,784)) # --> gives error if uncommented.
# test.summary() # --> gives error if uncommented.
test.fit(x_train, x_train, epochs=3, batch_size=64) # --> works well.
test.summary() # --> now it works fine.
如果我从模型中删除self.add_loss
,则可以正确构建模型。
在build
因自定义损失fit
设置模型之前,是否有办法self.add_loss()
?谢谢!
答案 0 :(得分:0)
这似乎是一个已知问题。尝试在此处https://github.com/tensorflow/tensorflow/issues/26274
进行检查在测试实例上传递样本输入数据即可达到目的。 在下面添加行。
const editByID = (id) => {
return axios.post('http://localhost:8000/getbyID',{ // Add return here
id:id
})
...(rest of code)...
这是使用google colab模拟的工作代码
class readstudent{
public static List<student> xyz=new List();
public static void main(String args[])
{
}
public static somemethod()
{
}
}