簡體   English   中英

使用 keras 擬合深度學習模型

[英]fit deep learning model using keras

我是深度學習和 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]

我的具體數據是: 在此處輸入圖片說明

這是 x 和 y 的形狀(以 * 分隔): 在此處輸入圖片說明 我真的不知道有什么問題。

我認為你需要改變 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 自動生成您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM