我很难将数据输入到简单的keras模型中以进行预测和训练。我已经看了相关的问题,但是花了6个小时尝试自己解决这个问题之后,我仍然在为此苦苦挣扎。
进口和网络:
from tensorflow import keras
from tensorflow.keras.layers import Dense
from tensorflow import convert_to_tensor
from random import randint
model = keras.Sequential()
model.add(Dense(3, activation='relu', input_dim=5))
model.add(Dense(4, activation='sigmoid'))
model.add(Dense(2))
model.compile(loss=keras.losses.MeanSquaredError(),
optimizer='adam',
metrics=['MeanSquaredError'])
尝试通过网络进行预测:
X = [4, 5, 2, 8, 9]
tensor = convert_to_tensor(X)
print(tensor.shape) # Prints (5, ) as expected
prediction = model.predict(tensor)
引发以下错误:
ValueError:检查输入时发生错误:预期density_input具有形状(5,),但具有形状为(1,)的数组
尝试训练网络:
states = [[7, 8, 3, 9, 7], [0, 7, 5, 0, 3], [2, 5, 8, 1, 10], [9, 9, 9, 8, 6]]
actions = [[1, 0], [0, 1], [0, 1], [0, 1]]
tensor_states = convert_to_tensor(states)
tensor_actions = convert_to_tensor(actions)
print(tensor_states.shape) # Prints (4, 5)
print(tensor_actions.shape) # Prints (4, 2)
model.fit(tensor_states, tensor_actions, verbose=0)
引发以下错误:
ValueError:TypeError:未调整大小的对象的len()
没有冗长= 0,我会收到此错误:
[AttributeError:'ProgbarLogger'对象没有属性'log_values']
我正在conda环境中运行。
我问了关于这个主题的previous question,但我意识到我使问题变得过于复杂,并且不确定自己要问的是什么。现在,我已经进行了更多的研究,我觉得我已经将问题归结为最基本的形式了。在考虑了this meta post之后,我写了这个新问题。如果我应该刚刚编辑上一个问题,请告诉我。
编辑-使用darcycp进行故障排除
正如下面给出的答案所指出的,我应该在专栏中给出预测网络的信息(使用两组括号)。但是,这会产生以下错误:
ValueError:TypeError:未调整大小的对象的len()
然后不使用张量(因为keras预测函数可以直接接受python列表,则会出现以下错误:
ValueError:尝试将类型()不支持的值(4)转换为张量
这对我来说非常不寻常,因为我没有将numpy导入该程序。