Value error while predicting result using Convlstm2d with attention layer? - tensorflow

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)

Related

problem while giving input to the model and saving the output to a file

I have a numpy array having the 512 data points, I want to consider this as the input to my 1d convolutional model.
After training the model in unsupervised way I want to predict the same input data from the model and want to save to a file
inp_data can be calculated as
import numpy as np
inp_data = np.random.normal(0, .1, 512)
So I did something like
inp_data = np.expand_dims(inp_data, axis=1) #512,1
img_width = inp_data.shape[1]
img_height = inp_data.shape[0]
input_img = Input((img_height,img_width))
print(input_img.shape) #(None, 512, 1)
x = Conv1D(256, 16, activation='relu', padding='same', strides=2)(input_img)
x = Conv1D(128, 16, activation='relu', padding='same',strides=2)(x)
x = BatchNormalization(name='bn1')(x)
x = Conv1D(64, 16, activation='relu', padding='same',strides=2 )(x)
encoded =Conv1D(32, 16, activation='relu', padding='same',strides=2 )(x)
x = Conv1DTranspose(32, 16,activation='relu', padding='same',strides=2 )(encoded)
x = Conv1DTranspose(64, 16, activation='relu', padding='same',strides=2 )(x)
x = BatchNormalization(name='bn3')(x)
x = Conv1DTranspose(128, 16, activation='relu', padding='same',strides=2)(x)
x = Conv1DTranspose(256, kernel_size=16, activation='relu', padding='same')(x)
decoded = Conv1DTranspose(1, 16, activation='sigmoid', padding='same',strides=2)(x)
autoencoder = Model(input_img, decoded)
sgd = tf.keras.optimizers.Adam(lr=0.001)
autoencoder.compile(optimizer=sgd, loss='mse')
autoencoder.summary()
batch = 1
history = autoencoder.fit(inp_data,inp_data, epochs=50, batch_size=batch, shuffle=True)
out = autoencoder.predict(inp_data)
print(out.shape)
np.savetxt('outfile',out)
However I am getting Errors:
WARNING:tensorflow:Model was constructed with shape (None, 512, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 512, 1), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (32, 1, 1).
(512, 16, 1)
and while trying to save the predicted output to a file I am getting errors like
np.savetxt('outfile',out)
File "<__array_function__ internals>", line 6, in savetxt
File "/home/yuanj/SFTR/anaconda3/envs/tfenv/lib/python3.7/site-packages/numpy/lib/npyio.py", line 1383, in savetxt
"Expected 1D or 2D array, got %dD array instead" % X.ndim)
ValueError: Expected 1D or 2D array, got 3D array instead
I hope experts may help me overcoming this problem.Thanks.

ValueError:Input 0 is incompatible with layer lstm_1: expected ndim=3,found ndim=4

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)

Neural network with 3d numpys as input and linear regression (one output)

I have already make some linear regression with NN and tensorflow but my input was a pandas dataframe (X_train).
Now I would like to create a neural network with coordinates. I will have X1(x1,y1) and X2(x2,y2) as input. X1.shape: 75, 2, 120 and X2.shape: 75, 2, 120 and y.shape: 75,1
What should be the architecture of the NN network ??
I have tried this:
print('Shape X1:', np.shape(X1))
Shape X1: (75, 2, 120)
model = keras.Sequential()
model.add(layers.Dense(50, input_dim=len(X1[1]), kernel_initializer='normal', activation='relu'))
model.add(layers.Dense(50, activation='relu'))
model.add(layers.Dense(1, activation="linear"))
model.summary()
loss = 'mse'
metric = ['mse']
model.compile(loss=loss,
optimizer= tf.keras.optimizers.Adam(learning_rate=0.001),
metrics=metric)
history = model.fit(X1, y, epochs=50, validation_split=0.3, verbose=1)
Here is the error i have got:
ValueError: Input 0 of layer sequential_6 is incompatible with the layer: expected axis -1 of input shape to have value 2 but received input with shape [None, 2, 120]
Change input shape. And you have to remove one dimension:
import tensorflow as tf
X1 = tf.random.uniform((75, 2, 120))
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(50, input_shape=(X1.shape[1], X1.shape[2]), kernel_initializer='normal', activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(50, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation="linear"))
model.summary()

Can not use convolution 1D . error : expected conv1d_1 to have 3 dimensions [duplicate]

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)

Merge layer keras with tensorflow backend

I wanted to merge two sequential models into one using a Merge layer but it is showing me an error. I am working with images, with size 128x128 (RGB image) and batch size is 32.
The error is:
ValueError: The model expects 3 input arrays, but only received one array. Found: array with shape (32, 3, 128, 128)
The model is defined as:
model = Sequential() leftBranch = Sequential()
leftBranch.add(Reshape((3,128,128), input_shape=(3, img_width, img_height)))
leftBranch.add(Convolution2D(14, 3, 1, activation='relu'))
leftBranch.add(ZeroPadding2D((1, 1)))
leftBranch.add(Flatten())
rightBranch = Sequential()
rightBranch.add(Reshape((3,128,128), input_shape=(3, img_width, img_height)))
rightBranch.add(Convolution2D(14, 1, 3, activation='relu'))
rightBranch.add(MaxPooling2D((2, 2), strides=(2, 2)))
rightBranch.add(Flatten())
centralBranch = Sequential()
centralBranch.add(Reshape((3,128,128), input_shape=(3, img_width, img_height)))
centralBranch.add(Convolution2D(14, 5, 5, activation='relu'))
centralBranch.add(MaxPooling2D((2, 2), strides=(2, 2)))
centralBranch.add(Flatten())
merged = Merge([leftBranch, centralBranch, rightBranch], mode='concat')
model = Sequential()
model.add(merged) model.add(Dense(64))
model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(1))
model.add(Activation('sigmoid'))
Error coming is :
ValueError: The model expects 3 input arrays, but only received one array. Found: array with shape (32, 3, 128, 128)
So, whats is the proper way to concatenate two sequential model with convolutional layers. I just want to Merge convolutional layers output like I did it here.