ValueError:没有为任何变量提供渐变:

我是 Tensorflow 2 的新手,我想在 keras/tensorflow 中训练多输入神经网络。这是我的示例代码:


First_inputs = Input(shape=(2000, ),name="first")

Second_inputs = Input(shape=(4, ),name="second")

embedding_layer = Embedding(3,3,  input_length=2000,)(First_inputs)

flatten = Flatten()(embedding_layer)

first_dense = Dense(neuronCount,kernel_initializer=initializer, )(flatten)

merge = concatenate([first_dense, Second_inputs])

drop = Dropout(dropout)(merge)

output = Dense(1, )(drop)

model = Model(inputs=[First_inputs, Second_inputs], outputs=output)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1,shuffle=True,  random_state=42)

First_inputs =x_train[:,0:2000]

Second_inputs =x_train[:,2000:2004]

model.fit(([First_inputs, Second_inputs], y_train),validation_data=([First_inputs, Second_inputs], y_train),verbose=1,epochs=100,steps_per_epoch=209)

但是,我收到此错误:


ValueError: No gradients provided for any variable: ['embedding/embeddings:0', 'dense/kernel:0', 'dense/bias:0', 'dense_1/kernel:0'].

有人知道问题是什么吗?谢谢!


人到中年有点甜
浏览 95回答 1
1回答

鸿蒙传说

您的数据是 numpy 数组,您必须为 fit() 方法提供两个单独的参数,np.arrays 列表作为输入,np.array 作为标签。(删除元组作为输入):First_inputs = Input(shape=(2000, ),name="first")Second_inputs = Input(shape=(4, ),name="second")embedding_layer = Embedding(3,3,  input_length=2000,)(First_inputs)flatten = Flatten()(embedding_layer)first_dense = Dense(neuronCount,kernel_initializer=initializer, )(flatten)merge = concatenate([first_dense, Second_inputs])drop = Dropout(dropout)(merge)output = Dense(1, )(drop)model = Model(inputs=[First_inputs, Second_inputs], outputs=output)x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1,shuffle=True,  random_state=42)First_inputs =x_train[:,0:2000]Second_inputs =x_train[:,2000:2004]model.fit([First_inputs, Second_inputs], y_train,validation_data=([First_inputs, Second_inputs], y_train),verbose=1,epochs=100,steps_per_epoch=209)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python