简体   繁体   English

使用 keras 拟合深度学习模型

[英]fit deep learning model using keras

I am new to deep learning and keras, I want to do a task which is : Train the model on the training data using 50 epochs.我是深度学习和 keras 的新手,我想做一个任务:使用 50 个时期在训练数据上训练模型。

I wrote this codes:我写了这个代码:

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)

but when I want to fit my model this way :但是当我想以这种方式拟合我的模型时:

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

I have this errors:我有这个错误:

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]

and my concrete data is :我的具体数据是: 在此处输入图片说明

and this is the shape of x and y (separated by *):这是 x 和 y 的形状(以 * 分隔): 在此处输入图片说明 I really have no idea what is the problem.我真的不知道有什么问题。

I think that you need to change input_shape like below:我认为你需要改变 input_shape 如下:

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

In the beginning, your data have include features and target data so the shape consists of both.一开始,您的数据包括特征和目标数据,因此形状由两者组成。 You need to minus 1 from that part to specify the input shape.您需要从该部分减去 1 以指定输入形状。

The other problem is you need to switch the data between x and y .另一个问题是您需要在xy之间切换数据。 I think that you want to predict Cement with rest of your dataset.我认为你想用你的数据集的其余部分来预测Cement So the Cement information should be stored in y and the rest of your dataset should be in x .因此, Cement信息应存储在y ,其余数据集应存储在x

Also, you need to change this part of the code.此外,您需要更改这部分代码。

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

Using the same data on the training and validation does not have meaning.在训练和验证中使用相同的数据没有意义。 You can specify the validation ratio so the keras will make you automatically.您可以指定验证比率,以便 keras 自动生成您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM