gpt4 book ai didi

python - 尝试理解 Keras 中的矩阵形状时遇到问题?

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:51 25 4
gpt4 key购买 nike

我正在学习 keras,这是我第一次在玩具示例中使用它,所以我尝试了以下线性回归,但是我得到了一个 ValueError: setting an array element with a sequence.:

在:

import pandas as pd
import keras
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation

dims = X.shape[1]
print(dims, 'dims')
print("Building model...")
nb_classes = y.shape[0]
print(nb_classes, 'classes')


model = Sequential()
model.add(Dense(1, input_dim=dims))
model.compile(optimizer='sgd', loss='mean_squared_error')

print(X.shape)
print(y.shape)
model.fit(X, y)

输出:

68 dims
Building model...
1000 classes
(1000, 68)
(1000,)
Epoch 1/10

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-10-7a58187d7756> in <module>()
19 print(X.shape)
20 print(y.shape)
---> 21 model.fit(X, y)

/usr/local/lib/python3.5/site-packages/keras/models.py in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, **kwargs)
662 shuffle=shuffle,
663 class_weight=class_weight,
--> 664 sample_weight=sample_weight)
665
666 def evaluate(self, x, y, batch_size=32, verbose=1,

/usr/local/lib/python3.5/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch)
1141 val_f=val_f, val_ins=val_ins, shuffle=shuffle,
1142 callback_metrics=callback_metrics,
-> 1143 initial_epoch=initial_epoch)
1144
1145 def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

/usr/local/lib/python3.5/site-packages/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, nb_epoch, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch)
841 batch_logs['size'] = len(batch_ids)
842 callbacks.on_batch_begin(batch_index, batch_logs)
--> 843 outs = f(ins_batch)
844 if not isinstance(outs, list):
845 outs = [outs]

/usr/local/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
1601 session = get_session()
1602 updated = session.run(self.outputs + [self.updates_op],
-> 1603 feed_dict=feed_dict)
1604 return updated[:len(self.outputs)]
1605

/usr/local/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
764 try:
765 result = self._run(None, fetches, feed_dict, options_ptr,
--> 766 run_metadata_ptr)
767 if run_metadata:
768 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
935 ' to a larger type (e.g. int64).')
936
--> 937 np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
938
939 if not subfeed_t.get_shape().is_compatible_with(np_val.shape):

/usr/local/lib/python3.5/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
529
530 """
--> 531 return array(a, dtype, copy=False, order=order)
532
533

ValueError: setting an array element with a sequence.

数据:

X.shape
(1000, 20)

标签:

y.shape
(1000,)

最佳答案

Dense()

input_shape 参数应该是一个整数。因此,请尝试以下操作:

model.add(Dense(nb_classes, input_dim=dims))

来自 official documentation :

keras.layers.core.Dense(output_dim, init='glorot_uniform', activation=None, weights=None, W_regularizer=None, b_regularizer=None, activity_regularizer=None, W_constraint=None, b_constraint=None, bias=True, input_dim=None)

在哪里

input_dim: dimensionality of the input (integer). This argument (or alternatively, the keyword argument input_shape) is required when using this layer as the first layer in a model.

关于python - 尝试理解 Keras 中的矩阵形状时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42026835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com