使用keras拟合深度学习模型

时间:2020-03-31 19:32:58

标签: python machine-learning keras deep-learning

我是深度学习和keras的新手,我想做的任务是:使用50个历元在训练数据上训练模型。

我写了以下代码:

import pandas as pd
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Dense
from sklearn.model_selection import train_test_split

concrete_data = pd.read_csv('https://cocl.us/concrete_data')

n_cols = concrete_data.shape[1]
model = Sequential()
model.add(Dense(units=10, activation='relu', input_shape=(n_cols,)))

model.compile(loss='mean_squared_error',
          optimizer='adam')


x = concrete_data.Cement
y = concrete_data.drop('Cement', axis=1)
xTrain, xTest, yTrain, yTest = train_test_split(x, y, test_size = 0.3)

但是当我想以这种方式拟合模型时:

model.fit(xTrain, yTrain, validation_data=(xTrain, yTrain), epochs=50)

我有此错误:

Epoch 1/50
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-83-489dd99522b4> in <module>()
----> 1 model.fit(xTrain, yTrain, validation_data=(xTrain, yTrain), epochs=50)

10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    966           except Exception as e:  # pylint:disable=broad-except
    967             if hasattr(e, "ag_error_metadata"):
--> 968               raise e.ag_error_metadata.to_exception(e)
    969             else:
    970               raise

ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:503 train_function  *
        outputs = self.distribute_strategy.run(
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:464 train_step  **
        y_pred = self(x, training=True)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:885 __call__
        self.name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:216 assert_input_compatibility
        ' but received input with shape ' + str(shape))

    ValueError: Input 0 of layer sequential_2 is incompatible with the layer: expected axis -1 of input shape to have value 9 but received input with shape [None, 1]

,我的具体数据是: enter image description here

,这是x和y的形状(用*分隔): enter image description here 我真的不知道是什么问题。

1 个答案:

答案 0 :(得分:2)

我认为您需要像下面这样更改input_shape:

input_shape=(n_cols,) =>>  input_shape=(n_cols-1,)

一开始,您的数据包含要素和目标数据,因此形状由两者组成。您需要从该部分减去1来指定输入形状。

另一个问题是您需要在xy之间切换数据。我认为您想用其余的数据集来预测Cement。因此,Cement信息应存储在y中,其余数据集应存储在x中。

此外,您需要更改代码的这一部分。

model.fit(xTrain, yTrain, validation_data=(xTrain, yTrain), epochs=50)

在培训和验证中使用相同的数据没有任何意义。您可以指定验证比例,以便keras自动使您成为现实。