Reputation: 645
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.
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 this is the shape of x and y (separated by *): I really have no idea what is the problem.
Upvotes: 0
Views: 290
Reputation: 1855
I think that you need to change input_shape like below:
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.
The other problem is you need to switch the data between x
and y
. I think that you want to predict Cement
with rest of your dataset. So the Cement
information should be stored in y
and the rest of your dataset should be in 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.
Upvotes: 2