Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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
Python Keras模型.predict-won';t接受大小为1(标量数)的输入_Python_Keras - Fatal编程技术网

Python Keras模型.predict-won';t接受大小为1(标量数)的输入

Python Keras模型.predict-won';t接受大小为1(标量数)的输入,python,keras,Python,Keras,我不熟悉Keras和python,现在我正在研究Keras,以找到数据模型并使用model.predict进行优化,但是model.predict只能将输入作为至少包含2个元素的numpy数组 我的代码是 import keras from keras.models import Sequential from keras.layers import Dense from keras.optimizers import SGD import numpy as np x = np.arange(

我不熟悉Keras和python,现在我正在研究Keras,以找到数据模型并使用model.predict进行优化,但是model.predict只能将输入作为至少包含2个元素的numpy数组

我的代码是

import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import numpy as np

x = np.arange(-2,3.0,0.01)
y = x**2 - 2*x + 1

model = Sequential()
model.add(Dense(50, activation='sigmoid', 
                input_dim=1, init='uniform'))
model.add(Dense(1, activation='linear'))
sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=False)
model.compile(loss='mean_squared_error', 
              optimizer='sgd',
              metrics=['accuracy'])
model.fit(x,y,nb_epoch=300, batch_size = 5,verbose = 0)
代码可以很好地拟合,但如果我尝试使用model.predict作为标量数,它会给我错误

(Pdb) model.predict(0.0)
*** Exception: Error when checking : data should be a Numpy array, or list/dict of Numpy arrays. Found: 0.0...
我强迫它是numpy数组,但还是失败了,它说输入需要是二维的

(Pdb) model.predict(np.asarray(0.0))
*** Exception: Error when checking : expected dense_input_1 to have 2 dimensions, but got array with shape ()
但如果我输入两个数字,它就会给出答案

(Pdb) model.predict([0.0,0.0])
array([[ 1.07415712],
       [ 1.07415712]], dtype=float32)
我需要模型。预测以单个数字作为输入用于优化。我不确定我使用的任何设置是否错误。请帮忙,谢谢。

试试:

model.predict(np.asarray(0.0).reshape((1,1)))

在Keras中,第一维度始终与示例编号相连,因此必须提供它。

示例编号指的是索引吗??例如,如果我必须输入一组特征,那么第一个维度是1。对于2组特征,第一个尺寸是2°??