为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

人到中年有点甜 2023-12-29 16:32:29
我是 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'].有人知道问题是什么吗?谢谢!
查看完整描述

1 回答

?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

您的数据是 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)


查看完整回答
反对 回复 2023-12-29
  • 1 回答
  • 0 关注
  • 94 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信