Python giving errors. How to solve it? - python

Here's the code:
import numpy as np
np.random.seed(123) #for reproducibility
from keras.models import sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(28,28,1)))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, Y_train,
batch_size=32, epochs=10, verbose=1)
score = model.evaluate(X_test, Y_test, verbose=0)
Here's the error:
Epoch 1/10
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-71-db342978fb82> in <module>()
37
38 model.fit(X_train, Y_train,
---> 39 batch_size=32, epochs=10, verbose=1)
40 score = model.evaluate(X_test, Y_test, verbose=0)
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
992 except Exception as e: # pylint:disable=broad-except
993 if hasattr(e, "ag_error_metadata"):
--> 994 raise e.ag_error_metadata.to_exception(e)
995 else:
996 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:853 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:842 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:835 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:787 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1020 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py:254 assert_input_compatibility
' but received input with shape ' + display_shape(x.shape))
ValueError: Input 0 of layer sequential_29 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape (32, 1, 28, 28)

Change
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
To,
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)

Related

Weird Error in Regression Model using Artificial Neural Networks in Tensorflow

I am running this code.
import math
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras import Model
from tensorflow.keras import Sequential
from tensorflow.keras.optimizers import Adam
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.layers import Dense, Dropout
from sklearn.model_selection import train_test_split
from tensorflow.keras.losses import MeanSquaredLogarithmicError
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
df_new = df_deduped.select_dtypes(include=numerics)
X = df_new[['Duration',
'Customers_Affected',
'Customers_Served',
'Total_Minutes',
'CostOfOutage',
'PercentAffected']]
y = df_new[['HT_Outages']]
print(X.head(1))
print(y.head(1))
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)
object= StandardScaler()
# standardization
X_train_scaled = object.fit_transform(X_train)
X_test_scaled = object.fit_transform(X_test)
hidden_units1 = 160
hidden_units2 = 480
hidden_units3 = 256
learning_rate = 0.01
# Creating model using the Sequential in tensorflow
def build_model_using_sequential():
model = Sequential([
Dense(hidden_units1, kernel_initializer='normal', activation='relu'),
Dropout(0.2),
Dense(hidden_units2, kernel_initializer='normal', activation='relu'),
Dropout(0.2),
Dense(hidden_units3, kernel_initializer='normal', activation='relu'),
Dense(1, kernel_initializer='normal', activation='linear')
])
return model
# build the model
model = build_model_using_sequential()
# loss function
msle = MeanSquaredLogarithmicError()
model.compile(
loss=msle,
optimizer=Adam(learning_rate=learning_rate),
metrics=[msle]
)
# train the model
history = model.fit(
x_train_scaled,
y_train,
epochs=25,
batch_size=64,
validation_split=0.2
)
def plot_history(history, key):
plt.plot(history.history[key])
plt.plot(history.history['val_'+key])
plt.xlabel("Epochs")
plt.ylabel(key)
plt.legend([key, 'val_'+key])
plt.show()
# Plot the history
plot_history(history, 'mean_squared_logarithmic_error')
Everything is fine up to this point. When I run the following line of code...
x_test['prediction'] = model.predict(x_test_scaled)
I get this error...
ValueError Traceback (most recent call last)
~\AppData\Local\Temp\1\ipykernel_23656\555877386.py in <module>
6 #df.head()
7
----> 8 x_test['prediction'] = model.predict(x_test_scaled)
~\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
~\Anaconda3\lib\site-packages\keras\engine\training.py in tf__predict_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "C:\Users\RS\Anaconda3\lib\site-packages\keras\engine\training.py", line 2137, in predict_function *
return step_function(self, iterator)
File "C:\Users\RS\Anaconda3\lib\site-packages\keras\engine\training.py", line 2123, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\RS\Anaconda3\lib\site-packages\keras\engine\training.py", line 2111, in run_step **
outputs = model.predict_step(data)
File "C:\Users\RS\Anaconda3\lib\site-packages\keras\engine\training.py", line 2079, in predict_step
return self(x, training=False)
File "C:\Users\RS\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\RS\Anaconda3\lib\site-packages\keras\engine\input_spec.py", line 277, in assert_input_compatibility
raise ValueError(
ValueError: Exception encountered when calling layer 'sequential_23' (type Sequential).
Input 0 of layer "dense_44" is incompatible with the layer: expected axis -1 of input shape to have value 18, but received input with shape (None, 1)
Call arguments received by layer 'sequential_23' (type Sequential):
• inputs=tf.Tensor(shape=(None, 1), dtype=float32)
• training=False
• mask=None
I am trying to follow the example from this link.
https://www.analyticsvidhya.com/blog/2021/08/a-walk-through-of-regression-analysis-using-artificial-neural-networks-in-tensorflow/?

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 81), found shape=(None, 77)

I am trying to train a neural network but I am getting the following error:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 81), found shape=(None, 77)
I tried to find the solution to this but am unable to do so. Can someone please help me?
Here Is the code of the same
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
from tensorflow.keras.callbacks import EarlyStopping
# Scaling the data
ss = StandardScaler()
X_train_sc = ss.fit_transform(X_train)
X_test_sc = ss.transform(X_test)
# Creating our model's structure
model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(81,)))
model.add(Dropout(0.18))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.15))
model.add(Dense(1, activation='sigmoid'))
es = EarlyStopping(monitor='val_loss', patience=5)
# Compiling the model
model.compile(loss='bce',
optimizer='adam',
metrics=['binary_accuracy'])
# Fitting the model
history = model.fit(X_train_sc,
y_train,
batch_size = 256,
validation_data =(X_test_sc, y_test),
epochs = 500,
verbose = 0,
callbacks=[es])
As suggested I have edited the code to:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
from tensorflow.keras.callbacks import EarlyStopping
import tensorflow as tf
samples = 500
X_train_sc = tf.random.normal((samples, 81))
y_train = tf.random.uniform((samples, ), maxval=2, dtype=tf.int32)
# Creating our model's structure
model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(81,)))
model.add(Dropout(0.18))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.15))
model.add(Dense(1, activation='sigmoid'))
es = EarlyStopping(monitor='val_loss', patience=5)
# Compiling the model
model.compile(loss='bce',
optimizer='adam',
metrics=['binary_accuracy'])
# Fitting the model
history = model.fit(X_train_sc,
y_train,
batch_size = 32,
epochs = 2,
verbose = 0)
But when I try to find the accuracy I get the same error as shown below:
# Scoring
train_score = model.evaluate(X_train_sc,
y_train,
verbose=1)
test_score = model.evaluate(X_test_sc,
y_test,
verbose=1)
labels = model.metrics_names
print('')
print(f'Training Accuracy: {train_score[1]}')
print(f'Testing Accuracy: {test_score[1]}')
16/16 [==============================] - 0s 2ms/step - loss: 0.6613 - binary_accuracy: 0.6040
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_7572/1082894889.py in <module>
3 y_train,
4 verbose=1)
----> 5 test_score = model.evaluate(X_test_sc,
6 y_test,
7 verbose=1)
~\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
~\Anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py in autograph_handler(*args, **kwargs)
1145 except Exception as e: # pylint:disable=broad-except
1146 if hasattr(e, "ag_error_metadata"):
-> 1147 raise e.ag_error_metadata.to_exception(e)
1148 else:
1149 raise
ValueError: in user code:
File "C:\Users\sadik\Anaconda3\lib\site-packages\keras\engine\training.py", line 1525, in test_function *
return step_function(self, iterator)
File "C:\Users\sadik\Anaconda3\lib\site-packages\keras\engine\training.py", line 1514, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\sadik\Anaconda3\lib\site-packages\keras\engine\training.py", line 1507, in run_step **
outputs = model.test_step(data)
File "C:\Users\sadik\Anaconda3\lib\site-packages\keras\engine\training.py", line 1471, in test_step
y_pred = self(x, training=False)
File "C:\Users\sadik\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\sadik\Anaconda3\lib\site-packages\keras\engine\input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_4" is incompatible with the layer: expected shape=(None, 81), found shape=(None, 77)
The problem is that your input data does not have the same shape that you defined in your first layer. Make sure the features dimension of your data corresponds to the input shape in the model's first layer. Here is an example:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
from tensorflow.keras.callbacks import EarlyStopping
import tensorflow as tf
samples = 500
# Create random dummy data
X_train_sc = tf.random.normal((samples, 81))
y_train = tf.random.uniform((samples, ), maxval=2, dtype=tf.int32)
X_test_sc = tf.random.normal((samples, 81))
y_test = tf.random.uniform((samples, ), maxval=2, dtype=tf.int32)
# Creating our model's structure
model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(81,)))
model.add(Dropout(0.18))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.15))
model.add(Dense(1, activation='sigmoid'))
# Compiling the model
model.compile(loss='bce',
optimizer='adam',
metrics=['binary_accuracy'])
# Fitting the model
history = model.fit(X_train_sc,
y_train,
batch_size = 32,
epochs = 2,
verbose = 0)
So, if your feature dimension is 77 then change input_shape=(81,) to input_shape=(77,).

ValueError: logits and labels must have the same shape ((None, 124, 124, 3) vs (None, 2))

I am developing a image classification model. I have my input shape of image as (128,128,3) but when I am running the model.fit it is giving an error.
My input data is
real_data = [f for f in os.listdir(data_dir+'/test') if f.endswith('.png')]
fake_data = [f for f in os.listdir(data_dir+'/test_f') if f.endswith('.png')]
print(real_data)
X = []
Y = []
for img in real_data:
X.append(img_to_array(load_img(data_dir+'/test/'+img)) / 255.0)
Y.append(1)
for img in fake_data:
X.append(img_to_array(load_img(data_dir+'/test_f/'+img)) / 255.0)
Y.append(0)
Y_val_org = Y
X = np.array(X)
Y = to_categorical(Y, 2)
print(X)
print(Y)
My model is
model = Sequential()
model.add(Conv2D(16, kernel_size=(3,3), activation='relu',input_shape=(128,128,3)))
model.add(Conv2D(16, kernel_size=(3,3), activation='relu'))
model.add(Dense(units=3, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer=optimizers.Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False),
metrics=['accuracy'])
#model.build(input_shape=(128,128,3))
model.summary()
And model summary is
Model: "sequential_80"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_892 (Conv2D) (None, 126, 126, 16) 448
_________________________________________________________________
conv2d_893 (Conv2D) (None, 124, 124, 16) 2320
_________________________________________________________________
dense_48 (Dense) (None, 124, 124, 3) 51
=================================================================
Total params: 2,819
Trainable params: 2,819
Non-trainable params: 0
_________________________________________________________________
When I am fitting the model through model.fit()
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=2, mode='auto')
EPOCHS = 20
BATCH_SIZE = 100
history = model.fit(X_train, Y_train, batch_size = BATCH_SIZE, epochs = EPOCHS, validation_data = (X_val, Y_val))
This is the error I am getting
Epoch 1/20
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-168-b3e2ed37ed88> in <module>()
2 EPOCHS = 20
3 BATCH_SIZE = 100
----> 4 history = model.fit(X_train, Y_train, batch_size = BATCH_SIZE, epochs = EPOCHS, validation_data = (X_val, Y_val))
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:756 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/compile_utils.py:203 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:152 __call__
losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:256 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:1608 binary_crossentropy
K.binary_crossentropy(y_true, y_pred, from_logits=from_logits), axis=-1)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4979 binary_crossentropy
return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/nn_impl.py:174 sigmoid_cross_entropy_with_logits
(logits.get_shape(), labels.get_shape()))
ValueError: logits and labels must have the same shape ((None, 124, 124, 3) vs (None, 2))
Change your model into:
model.add(Conv2D(16, kernel_size=(3,3), activation='relu'))
model.add(Flatten()) # added flatten before dense
model.add(Dense(units=2, activation='softmax'))
Last output should be 2 units because you have 2 classes. Also change your loss to:
loss='categorical_crossentropy'
because you applied to_categorical().

How do I overcome the problem, model fit in python

I'm studying the machine learnig. It's interesting!
I have a question about the error.
I share the code and error message, below.
Please resolve it..! Thank you very much!
The error shows me the value error input 0 layer if the sequential_4...
a=df4['age']
b=df4['growth']
X=np.array(a.values.tolist())
y=np.array(b.values.tolist())
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split
import numpy
import tensorflow as tf
seed = 0
numpy.random.seed(seed)
tf.random.set_seed(3)
X_train, X_test, y_train, y_test = train_test_split(a, b,
test_size = 0.3, random_state=seed)
model = Sequential()
model.add(Dense(30, input_dim=17, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_sqaured_error',
optimizer='adam')
model.fit(X_train, y_train, validation_data= (X_test, y_test), epochs=200, batch_size=10)
error message
Epoch 1/200
ValueError Traceback (most recent call last)
<ipython-input-56-ffc8e137fb64> in <module>()
----> 1 model.fit(X_train, y_train, validation_data= (X_test, y_test), epochs=200, batch_size=10)
9 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 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:2730 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:3417 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:754 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:259 assert_input_compatibility
' but received input with shape ' + display_shape(x.shape))
ValueError: Input 0 of layer sequential_14 is incompatible with the layer: expected axis -1 of input shape to have value 17 but received input with shape (None, 1)
I was able to replicate your issue using sample code shown below
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split
X = np.random.random((1000,1))
y = np.random.random((1000,1))
X_train,X_test, y_train,y_test = train_test_split(X,y)
dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
train_data = dataset.shuffle(len(X_train)).batch(32)
train_data = train_data.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
valid_ds = tf.data.Dataset.from_tensor_slices((X_test, y_test))
model = Sequential()
model.add(Dense(30, input_dim=17, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_sqaured_error',
optimizer='adam')
model.fit(train_data, epochs=3, validation_data=valid_ds)
Output:
Epoch 1/3
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-0e4d5121895c> in <module>()
30
31 #model.fit(X_train, y_train, validation_data= (X_test, y_test), epochs=200, batch_size=10)
---> 32 model.fit(train_data, epochs=3, validation_data=valid_ds)
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:754 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py:259 assert_input_compatibility
' but received input with shape ' + display_shape(x.shape))
ValueError: Input 0 of layer sequential_2 is incompatible with the layer: expected axis -1 of input shape to have value 17 but received input with shape (None, 1)
Fixed code:
Here input layer of your sequential model has to be setup to 1 instead of 17 because, shape of your input data is (None, 1).
You can specify loss function as mse instead of mean_sqaured_error in model.compile.
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.model_selection import train_test_split
X = np.random.random((1000,1))
y = np.random.random((1000,1))
X_train,X_test, y_train,y_test = train_test_split(X,y)
dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
train_data = dataset.shuffle(len(X_train)).batch(32)
train_data = train_data.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
valid_ds = tf.data.Dataset.from_tensor_slices((X_test, y_test))
model = Sequential()
model.add(Dense(30, input_dim=1, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1))
model.compile(loss='mse',
optimizer='adam')
model.fit(train_data, epochs=3, validation_data=valid_ds)
Output:
Epoch 1/3
24/24 [==============================] - 1s 28ms/step - loss: 0.5050 - val_loss: 0.2758
Epoch 2/3
24/24 [==============================] - 0s 21ms/step - loss: 0.2704 - val_loss: 0.1908
Epoch 3/3
24/24 [==============================] - 0s 21ms/step - loss: 0.2047 - val_loss: 0.1454
<tensorflow.python.keras.callbacks.History at 0x7fc28239d2d0>

ValueError: Input 0 of layer sequential_37 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 15]

I've made all the attempts I knew.
Also all combinations of input_dim = 15 already. If anyone can help me?
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
(233941, 15)
(233941, 1)
(100261, 15)
(100261,)
I've already done the test using input_dim = (233941, 15) and input_dim = (233941, 1). But I still can't find the problem.
Can my problem be in the dataset division?
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dense, Dropout, LSTM
model = Sequential()
model.add(LSTM(100, input_dim=15, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(50, return_sequences = True))
model.add(Dropout(0.3))
#3 camada
model.add(LSTM(50, return_sequences = True))
model.add(Dropout(0.3))
model.add(LSTM(units = 50))
model.add(Dropout(0.3))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(optimizer = 'adam', loss = 'mean_squared_error',
metrics = ['mean_absolute_error'])
# Fit the model
model.fit(x_train,y_train,epochs=100, validation_data=(x_test,y_test))
Epoch 1/100
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-79-2e6c4d489e38> in <module>()
21 metrics = ['mean_absolute_error'])
22 # Fit the model
---> 23 model.fit(x_train,y_train,epochs=100, validation_data=(x_test,y_test))
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
971 except Exception as e: # pylint:disable=broad-except
972 if hasattr(e, "ag_error_metadata"):
--> 973 raise e.ag_error_metadata.to_exception(e)
974 else:
975 raise
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 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:2585 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:2945 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:747 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__
self.name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:180 assert_input_compatibility
str(x.shape.as_list()))
ValueError: Input 0 of layer sequential_37 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 15]
As Marco Certliani mentioned in the comments you need to format your input properly for a RNN, which as the error you got mentioned is 3 dimensional.
Here is a representation of what the input tensor should look like:
Meaning that your 3D tensor will have a shape of (batch_size, time_step, input_dimension).

Categories

Resources