i have function that creates a ann model
def create_model(input_dim, n_action):
""" A multi-layer perceptron """
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv1D(
128,
kernel_size=5,
padding='same',
activation=tf.keras.activations.relu,
input_shape=(101,)
))
model.add(tf.keras.layers.Conv1D(
64,
kernel_size=5,
padding='same',
activation=tf.keras.activations.relu,
))
model.add(tf.keras.layers.Dense(
64,
activation=tf.keras.activations.relu,
))
model.add(
tf.keras.layers.Dense(
64,
activation=tf.keras.activations.relu,
))
model.add(
tf.keras.layers.Dense(
32,
activation=tf.keras.activations.relu,
))
model.add(
tf.keras.layers.Dense(
n_action,
activation=tf.keras.activations.relu,
))
model.compile(loss='mse', optimizer='adam')
print((model.summary()))
return model
as you can see the first layer is a 1d convolutional layer with filters 128 and kelrnel size 5.
my dataset is just an array of numbers of length 101
[584.95 582.3 581.7 ... 392.35 391.8 391.3 ]
but when i do
model.train_on_batch(states, target_full)
where states are arrays of length 101 and batch size 32
i get this error.
Input 0 of layer "conv1d_118" is incompatible with the layer: expected min_ndim=3, found ndim=2. Full shape received: (None, 101)
I don't understand why dose a 1dconv need 3 dimensional input.Can anyone help me solve this error?
Any help is very much appreciated.
Related
BATCH_SIZE = 32 # ADVISED NOT TO CHANGE THIS
N_PAST = 10 # DO NOT CHANGE THIS
N_FUTURE = 10 # DO NOT CHANGE THIS
SHIFT = 1
model = tf.keras.models.Sequential([
tf.keras.layers.Conv1D(filters=32, kernel_size=5,
strides=1, padding="causal",
activation="relu",
input_shape=[None, 1]),
tf.keras.layers.LSTM(64, return_sequences=True),
tf.keras.layers.LSTM(64, return_sequences=True),
tf.keras.layers.Dense(30, activation="relu"),
tf.keras.layers.Dense(10, activation="relu"),
tf.keras.layers.Dense(N_FEATURES)
])
I build a time-siries forcasting model but i can't understand how to handle this.
which layer should i use to make this available?
Model input shape must be (BATCH_SIZE, N_PAST = 10, N_FEATURES = 1)
Model output shape must be (BATCH_SIZE, N_FUTURE = 10, N_FEATURES = 1)
the batch_size is not specified in the model.
model = tf.keras.models.Sequential([
tf.keras.layers.Conv1D(filters=32, kernel_size=5,
strides=1, padding="causal",
activation="relu",
input_shape=[N_PAST,1]),
tf.keras.layers.LSTM(64, return_sequences=True),
tf.keras.layers.Dense(30, activation="relu"),
tf.keras.layers.Dense(10, activation="relu"),
tf.keras.layers.Dense(N_FUTURE)
])
You don't need to specify batch_size in input layer. Just change input shape as follows:
input_shape=[N_PAST,1]
class SmallerVGGNet:
#staticmethod
def build(width, height, depth, classes, finalAct="softmax"):
# initialize the model along with the input shape to be
# "channels last" and the channels dimension itself
model = Sequential()
inputShape = (height, width, depth)
chanDim = -1
units = 1
if K.image_data_format() == "channels_first":
inputShape = (depth, height, width)
chanDim = 1
# CONV => RELU => POOL
model.add(Conv2D(16, (3, 3), padding="same", input_shape=inputShape,))
model.add(BatchNormalization(axis=chanDim))
model.add(Activation("relu"))
model.add(
LSTM(128, activation='tanh', return_sequences=True, use_bias=True, kernel_initializer="glorot_uniform"))
# softmax classifier
model.add(Flatten())
model.add(Dropout(0.5))
print(model.summary())
return model
How to fix this error ? str(x.shape.as_list()))
ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 16, 16, 128]
LSTM expects inputs: A 3D tensor with shape [batch, timesteps, feature]
Working sample code
inputs = tf.random.normal([32, 10, 8])
lstm = tf.keras.layers.LSTM(4)
output = lstm(inputs)
print(output.shape)
Output
(32, 4)
I want to build a CNN model that takes 3 successive images insetead of one, so the input takes the shape: (3,height, width, channels=3) :
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Dropout, Dense,
Flatten,Convolution2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
def build_cnn_model(frames_number,height,width,channel, nb_actions):
model = Sequential()
model.add( Input((frames_number,height,width,channel),name='Input') )
model.add( Conv2D(96, (3,3), strides=(4,4), activation='relu', name='Conv2D_1',
input_shape = (frames_number,height,width,channel) ) )
model.add( MaxPooling2D((2, 2), name='MaxPooling2D_1') )
model.add( Dropout(0.2,name='Dropout_1'))
model.add( Conv2D(192, (3, 3), activation='relu', name='Conv2D_2') )
model.add( MaxPooling2D((2, 2), name='MaxPooling2D_2') )
model.add( Dropout(0.2, name='Dropout_2'))
model.add( Flatten(name='Flatten_1'))
model.add( Dense(1500, activation='relu', name='Dense_1') )
model.add( Dropout(0.5, name='Dropout_DNN_1'))
model.add(Dense(nb_actions, activation='linear', name='Output') )
return model
model = build_cnn_model(3,220,300,3,6)
The structure seems to be logic for me, but I got :
ValueError: Input 0 of layer Conv2D_1 is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 3, 210, 160, 3]
Note, I know it is possible also to change the data shape, so that the 3 images can be put in one single image of 3*3 channels. but I can't apply that solution in my program. I want to passe input of (3, height, width, 3).
I am integrating Attention mechanism with convlstm2d. I can build and fit the model but getting value error while predicting the result.
I am using attention layer implementation of from:
https://www.kaggle.com/qqgeogor/keras-lstm-attention-glove840b-lb-0-043
X.shape
#(6766, 8, 100)
n_features = 100
n_seq = 4
n_steps = 2
X = X.reshape((X.shape[0], n_seq, 1, n_steps, n_features))
#(6766, 4, 1, 2, 100)
model = Sequential()
model.add(ConvLSTM2D(filters=32, kernel_size=(1,2),return_sequences=True, activation='relu', input_shape=(n_seq, 1, n_steps, n_features)))
model.add(Attention(n_steps))
model.add(Dense(100, activation='relu'))
model.compile(optimizer='adam', loss='mse')
model.summary()
ConvLSTM2D- output shape =(None, 4, 1, 1, 32)
Attention- output shape =(None, 32)
Dense - output shape =(None, 100)
This the error what I am getting:
ValueError: could not broadcast input array from shape (4096,100) into shape (32,100)
This question already has answers here:
Dimension of shape in conv1D
(5 answers)
Closed 5 years ago.
I use convolution 1D like this.
X_train[:, None].shape
X_train_t = X_train[:, None]
X_test_t = X_test[:, None]
K.clear_session()
model = Sequential()
model.add(Conv1D(39, 1, activation='relu', input_shape=(1,12)))
model.compile(loss='mean_squared_error', optimizer='adam' )
model.summary()
model.fit(X_train_t, y_train, epochs=200, batch_size=1, verbose=1)
y_pred = model.predict(X_test)
It show error like this
ValueError: Error when checking target: expected conv1d_1 to have 3 dimensions, but got array with shape (39, 1)
I print shape with this code print(X_train.shape) it show .
(39, 12)
If I change input_shape model to 1,1 .
model.add(Conv1D(39, 1, activation='relu', input_shape=(1,1)))
It show error.
ValueError: Error when checking input: expected conv1d_1_input to have shape (None, 1, 1) but got array with shape (39, 1, 12)
How to use convolution 1D ?
Add model.add(Flatten()) in your code.
model = Sequential()
model.add(Flatten())
model.add(Conv1D(39, 1, activation='relu', input_shape=(1,12)))
model.compile(loss='mean_squared_error', optimizer='adam' )
model.summary()
model.fit(X_train_t, y_train, epochs=200, batch_size=1, verbose=1)
y_pred = model.predict(X_test)