【问题标题】:Theano input and output sample number errorTheano输入输出样本数错误
【发布时间】:2016-05-10 14:57:04
【问题描述】:

我正在做一些项目,我需要在 python 中使用神经网络。我正在尝试训练神经网络,但 FIT() 函数总是出错。这是我的代码:

def matrix_to_vector(m):
    return m.flatten()


def prepare_for_rnn(tones):
    ready_for_rnn = []
    for tone in tones:
        ready_for_rnn.append(matrix_to_vector(tone))

    return ready_for_rnn

def convert_output2(outputs):
    return np.eye(len(outputs))

tone = LoadDataSet('samples/ddur.wav')

X_train = []
X_train.append(tone.DataSet)


x_train = prepare_for_rnn(X_train)
tones = ['D']
y_train = convert_output2(tones)

model = Sequential()

model.add(Dense(128, input_dim=1, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))

sgd = SGD(lr=0.01, momentum=0.9)
model.compile(loss='mean_squared_error', optimizer=sgd)

y_train = np.array(y_train)

print x_train
print y_train
print y_train.shape
print len(y_train)

model.fit(x_train, y_train, nb_epoch=2000, batch_size=1, verbose=0, shuffle=False, show_accuracy=False)
score = model.evaluate(x_train, y_train, batch_size=16)

我收到错误消息,我的输入数组和输出数组的样本数不同。

我的控制台输出:

/usr/bin/python2.7 /home/misel/PycharmProjects/SoftProjekat/main.py
Using Theano backend.
[array([ 0.70347332,  0.72311571,  2.64259667, ...,  0.52694423,
        0.21127148,  0.43055696])]
[[ 1.]]
(1, 1)
1
Traceback (most recent call last):
  File "/home/misel/PycharmProjects/SoftProjekat/main.py", line 103, in <module>
    model.fit(x_train, y_train, nb_epoch=2000, batch_size=1, verbose=0, shuffle=False, show_accuracy=False)
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 503, in fit
    raise Exception('All input arrays and the target array must '
Exception: All input arrays and the target array must have the same number of samples.

【问题讨论】:

  • model.add(Dense(1, input_dim=128, activation='sigmoid'))
  • 这也是多余的model.add(Dense(1, activation='sigmoid'))
  • 可能希望 y_train 成为 [1] 而不是 [[1]]

标签: python neural-network theano keras


【解决方案1】:

当我遇到“异常:所有输入数组和目标数组必须具有相同数量的样本”的问题时,我正在实施自动编码器。 model.fit 期望 numpy 数组作为输入。

X: data, as a numpy array.
y: labels, as a numpy array.

将列表转换为 numpy 数组解决了我的问题。

如果我们看到 model.fit 的文档,它会说:

if type(X) == list:
            if len(set([len(a) for a in X] + [len(y)])) != 1:
                raise Exception('All input arrays and the target array must ''have the same number of samples.')

所以如果 X 是列表类型:列表 X 中所有项目的长度和列表 y 的长度应该相同,否则将引发异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多