Set input shape of model in keras - python

I had saw other similar question on tensor flow but didn't match my problem.
Model:
# picture size
img_row = 128
img_col = 647
shape = (img_row, img_col)
img = Input(input_shape)
...
with result
Data:
There has 1000 datas and each with shape (128, 647), and its a column of Dataframe df.
Therefore, size result and data preview are as follow:
Problem
The problem is: when I pass the Data to Model, some size error occured.
train_history = model.fit( x = df["data"],
y = df["genre_idx"],
validation_split = 0.1,
epochs = 30,
batch_size = 200,
verbose = 2
)
And error message are as follow:
Error when checking input: expected input_79 to have 3 dimensions, but got array with shape (1000, 1)
It might be a low question, but I didn't figure out what is the main problem of this situation and how to solve it.

You need to give it as a single ndarray which you can extract using the .values property of the data frame. The expected shape for the input is (1000, 128, 647).

Related

Reshape Tensorflow model batch dimension into time series

I'm trying to reshape a Tensorflow model's input along the batch dimension. I want to combine some of the batch samples into a time-series so I can feed it into an LSTM layer.
Specifically, I have 1024 samples and I'd like to put them into groups of 64 timesteps with the result being 16 batches of 64 timesteps, each timestep having the original 24 features.
#input tensor is (1024, 24)
inputLayer = Input(shape=(24,))
#I want it to be (16, 64, 24)
reshapedLayer = layers.Reshape([64, 24])(inputLayer)
lstmLayer = layers.LSTM(128, activation='relu')(reshapedLayer)
This compiles but throws a runtime error
tensorflow.python.framework.errors_impl.InvalidArgumentError:
Input to reshape is a tensor with 24576 values, but the requested shape has 1572864
I understand what the error is telling me, but I'm not sure the right way to go about fixing it.
Perhaps this could work for you:
import tensorflow as tf
inputs = tf.keras.layers.Input(shape=(24,))
x = tf.reshape(inputs, (16, 64, 24))
x = tf.keras.layers.LSTM(128, activation='relu')(x)
model = tf.keras.Model(inputs=inputs, outputs=x)
# dummy data
inputs = tf.random.uniform(shape=(1024, 24))
outputs = model(inputs)
Replacing the Reshape layer with tf.reshape.

how to set appropriate input shape of model in Keras

I'm a newbie to Keras. I'm playing around Keras to get some intuition and stuck with here.
input_image = tf.keras.Input(shape=(16,16,3))
x = tf.keras.layers.Conv2D(32,(3,3), padding = 'same')(input_image)
model = tf.keras.Model(input_image , x)
model.compile(optimizer='Adam',loss = 'MSE')
inputs = np.random.normal(size = (16,16,3))
outputs = np.random.normal(size = (16,16,32))
model.fit(x = inputs , y =outputs)
I just wanted to see the output shape that model.summary says (None, 16, 16, 32). But now I have two questions. One is the output shape and another is why my code doesn't work. I hope someone tells me what I'm missing. Thanks~
inputs = np.random.normal(size = (1,16,16,3)) #<---- here
outputs = np.random.normal(size = (1,16,16,32)) #<---here
They should be 4D not 3D in shape. You need to give the detail of batch also.
(batch_size, w,h,c) <---- 4D
You are missing batch_size
32,(3,3) from tf.keras.layers.Conv2D(32,(3,3), padding = 'same')(input_image)
You have 32 filters. So the channel depth will be 32. But since you have used the padding='same' so your output will have the same dimension as input. Only differ in depth.

How can I put multiple output for keras DL training?

I want to make a network that uses multiple output.
For example, I want to put an input of list which has the shape of :
[ 8, 128, 128, 3]
Here, 8 is number of images in one set of input, 128 x 128 x 3 is shape of a color image.
And I want my output to be :
[ 8, 128, 128]
Here, 8 is number of images in one set of output, 128 x 128 is shape of a gray image.
So I made my code as following :
f_list = []
for i in range(v):
img_in = M.Input((h, w, 3)) # Input :
feature = spm.encoder(img_in)
f_list.append(feature)
fuse_ave,fuse_max,fuse_min = spm.fusion(f_list)
decode_list = []
for i in range(v):
ffuse = spm.decoder(f_list[i],fuse_ave,fuse_max)
decode_list.append(ffuse)
dl_con = L.concatenate(decode_list,0)
print(dl_con.shape)
epsnet = M.Model(inputs = img_in,outputs = dl_con)
epsnet.compile(optimizer=O.Adam(lr=0.0001,decay = 0.000001), loss='mean_squared_error', metrics=['accuracy'])
epsnet.fit(iml,np.array(gml),batch_size = 5, epochs=50,verbose=1,shuffle=True, validation_split=0.1)
Here, the function decode is as following :
def decoder(input,fuse_a,fuse_M): #input : encoded
infu = L.Concatenate(-1)([input,fuse_a,fuse_M])
f=128
x = L.Conv2DTranspose(filters = f,kernel_size=(5,5),strides=2,padding = 'same')(infu) dding = 'same')(x__)
...
x__ = L.BatchNormalization()(x__)
x__ = L.Activation('relu')(x__)
def sq(x):
x_sq = B.squeeze(x,-1)
return x_sq
xq = L.Lambda(sq)(x__)
return xq
Here I am getting error message :
ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer` (thus holding past layer metadata). Found: Tensor("concatenate_7/concat:0", shape=(?, ?, ?), dtype=float32)
I tried several ways, but I still get same error message. Please give me a breakthrough and thank you very much.

ValueError: Error when checking input: expected input_1 to have shape (168, 5) but got array with shape (5808, 5)

I'm trying to implement a hybrid LSTM-DNN forecaster with multiple inputs using the code from Hvass-Labs Time Series tutorial #23. Basically I want to forecast day-ahead prices (just a 24 time step into the future for now) of electricity using sequential and non-sequential data. The model I'm using is two sets of inputs feeding an LSTM (for the sequential data) and Dense for the non-sequential data, with their outputs concatenated. It looks like this:
!https://imgur.com/a/x15FfIy
Basically whenever I try to fit the model after one epoch it shows this error:
UPDATE:
ValueError: Error when checking input: expected input_1 to have shape (168, 5) but got array with shape (5808, 5)
The changes I have implemented:
# Chop off x_test_scaled into two parts:
x_test1_scaled = x_test_scaled[:,0:5] # shape is (5808, 5)
x_test2_scaled = x_test_scaled[:,5:12] # shape is (5808, 7)
validation_data = [np.expand_dims(x_test1_scaled, axis=0), np.expand_dims(x_test2_scaled, axis=0)], np.expand_dims(y_test_scaled, axis=0)
I'm confused because I have indeed assigned the generator to the generator in the model.fit_generator, and I'm not passing the x_test1_scaled which does have the shape of (5808, 5). edit:(not validation_data)
%%time
model.fit_generator(generator=generator,
epochs=10,
steps_per_epoch=30,
validation_data=validation_data,
callbacks=callbacks)
If this helps, this is my model:
# first input model
input_1 = Input(shape=((168,5)))
dense_1 = Dense(50)(input_1)
# second input model
input_2 = Input(shape=((168,7)))
lstm_1 = LSTM(units=64, return_sequences=True, input_shape=(None, 7,))(input_2)
# merge input models
merge = concatenate([dense_1, lstm_1])
output = Dense(num_y_signals, activation='sigmoid')(merge)
model = Model(inputs=[input_1, input_2], outputs=output)
# summarize layers
print(model.summary())
EDIT: Cleared this problem, replaced with error on top.
Thus far I've managed everything up to actually fitting the model.
Whenever an epoch finishes however it goes into the error:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[0.4 , 0.44444442, 0. , ..., 0.1734707 ,
0.07272629, 0.07110982],
[0.3904762 , 0.43434343, 0.04347826, ..., 0.1740398 ,
0.07282589, 0.06936309],
...
I have tried the solutions from other stackexchange posts of the same error message. They haven't been successful, but I was able to eventually isolate the problem array to that of the validation_data. I just don't know how to "reshape" it into the required 2 array.
The batch generator: I have included the two sets of inputs already. the x_batch_1 and x_batch_2
def batch_generator(batch_size, sequence_length):
"""
Generator function for creating random batches of training-data.
"""
# Infinite loop.
while True:
# Allocate a new array for the batch of input-signals.
x_shape = (batch_size, sequence_length, num_x_signals)
x_batch = np.zeros(shape=x_shape, dtype=np.float16)
# Allocate a new array for the batch of output-signals.
y_shape = (batch_size, sequence_length, num_y_signals)
y_batch = np.zeros(shape=y_shape, dtype=np.float16)
# Fill the batch with random sequences of data.
for i in range(batch_size):
# Get a random start-index.
# This points somewhere into the training-data.
idx = np.random.randint(num_train - sequence_length)
# Copy the sequences of data starting at this index.
x_batch[i] = x_train_scaled[idx:idx+sequence_length]
y_batch[i] = y_train_scaled[idx:idx+sequence_length]
x_batch_1 = x_batch[ :, :, 0:5]
x_batch_2 = x_batch[ :, :, 5:12]
yield ([x_batch_1, x_batch_2], y_batch)
batch_size = 32
sequence_length = 24 * 7
generator = batch_generator(batch_size=batch_size,
sequence_length=sequence_length)
Validation set:
validation_data = np.expand_dims(x_test_scaled, axis=0), np.expand_dims(y_test_scaled, axis=0)
And lastly the model fit:
%%time
model.fit_generator(generator=generator,
epochs=10,
steps_per_epoch=30,
validation_data=validation_data,
callbacks=callbacks)
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[0.4 , 0.44444442, 0. , ..., 0.1734707 ,
0.07272629, 0.07110982],
[0.3904762 , 0.43434343, 0.04347826, ..., 0.1740398 ,
0.07282589, 0.06936309],
...
The array is the same one as the validation_data. Another thing is that the error creeps up whenever the first epoch finishes which strengthens the case for the problem being the validation_data.
It's because your model need 2 sets of input, x_batch_1, x_batch_2 in your batch_generator. While your validation_data has only one array np.expand_dims(x_test_scaled, axis=0)
You need to make validation_data looks like the batch_generator, probably [np.expand_dims(x_test1_scaled, axis=0), np.expand_dims(x_test2_scaled, axis=0)], np.expand_dims(y_test_scaled, axis=0).
In case of you still don't understand, please provide information about x_test1_scaled, like it's shape or how you load it.

Shapes of logits and labels are incompatible

The full error message is like this:
ValueError: Shapes (2, 1) and (50, 1) are incompatible
It occurs when my model is trained. The mistake either is in my input_fn:
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x = {"x" : training_data},
y = training_labels,
batch_size = 50,
num_epochs = None,
shuffle = True)
in my logits and loss function:
dense = tf.layers.dense(inputs = pool2_flat, units = 1024, activation = tf.nn.relu)
dropout = tf.layers.dropout(inputs = dense, rate = 0.4, training = mode == tf.estimator.ModeKeys.TRAIN)
logits = tf.layers.dense(inputs = dropout, units = 1)
loss = tf.losses.softmax_cross_entropy(labels = labels, logits = logits)
or in my dataset. I can only print out the shape of my dataset for you to take a look at it.
#shape of the dataset
train_data.shape
(1196,2,1)
train_data[0].shape
(2,1)
#this is the data
train_data[0][0].shape
(1,)
train_data[0][0][0].shape
(20,50,50)
#this is the labels
train_data[0][1].shape
(1,)
The problem seems to be the shape of the logits. They are supposed to be [batch_size, num_classes] in this case [50,1] but are [2,1]. The shape of the labels is correctly [50,1]
I have made a github gist if you want to take a look at the whole code.
https://gist.github.com/hjkhjk1999/38f358a53da84a94bf5a59f44050aad5
In your code, you are stating that the inputs to your model will be feed in batches of 50 samples per batch with one variable. But it looks like your are feeding actually a batch of 2 samples with 1 variable (shape=[2, 1]) despite feeding labels with shape [50, 1].
That's the problem, you are giving 50 'questions' and two 'answers'.
Also, your dataset is shaped in a really weird way. I see you named your github gist 3D Conv. If you are indeed trying to do a 3D convolution you might want to reshape your dataset into a tensor (numpy array) of this shape shape = [samples, width, height, deepth]

Categories

Resources