i am basically trying to build a deep model which consists of many convolution2d layers followed by maxpooling 2d as follows :
model.add(Convolution2D(128, 54, 7, input_shape=(1, 54, 180)))
model.add(MaxPooling2D(pool_size=(1, 3)))
model.add(Convolution2D(128, 1, 7))
model.add(MaxPooling2D(pool_size=(1, 3)))
However , i am getting the following error :
File
"/home/user/anaconda2/lib/python2.7/site-packages/keras/engine/training.py",
line 100, in standardize_input_data
str(array.shape)) Exception: Error when checking model input: expected convolution2d_input_1 to have 4 dimensions, but got array
with shape (8000, 180, 54)
Blockquote
But i am following the (samples, channels, rows, cols) norm. Why is this happening ?
It seems like your input data has the wrong shape. You should print out the shape of the data that you are feeding into the network.
It seems like your array are gray input images and they normally only use 2 dimensions because they only have 1 channel. Therefore the np array is ordered without the third dimension. Normally you have to add this by using np.reshape or allocating your array in another way. When I get an error message like yours i would try:
X # training data
X = np.transpose(X, (0, 2, 1))
X = np.reshape(X, (X.shape[0], 1, X.shape[1], X.shape[2]))
Related
I’m trying to run a image fourier transformation with various output sizes of signal using pytorch. For example,suppose that the input size of image is [3x32x32] and the output size of fourier transformation is [3x64x64], then ideal code can be represented as torch.rfft(input, signal_ndim=3, n=(3,64,64)) (if given n is the output size of signal).
However, pytorch doesn’t seem to offer the argument ‘n’. According to the pytorch fft documentation, N represents the output size of signal, but cannot be modified in the code.
SO, does anyone know how to change the output size of fourier transformed signal using pytorch? I would appreciate if anyone can tell me a solution to change the output size of fourier trasformed signal.
You can see that numpy-fft library uses the argument ‘s’ as the output size of the signal in the code.
Numpyhttps://numpy.org/doc/stable/reference/generated/numpy.fft.fftn.htmlnumpy.fft.fftn(a, s=None, axes=None, norm=None)
-> ‘s’ denotes shape of the output of signal.Simple code
import torch
import numpy as np
np_a = torch.rand(3, 32, 32).numpy()
np_a_ft = np.fft.fftn(a, (3,28, 28))
print(np_a_ft.shape)
Input size of image : 3, 32, 32
Output size of after fourier transform : 3, 28, 28
Pytorchhttps://pytorch.org/docs/stable/generated/torch.rfft.htmltorch.rfft(input, signal_ndim, normalized=False, onesided=True)
torch_x = torch.rand(3, 32, 32, requires_grad=True)
torch_xf = torch.rfft(torch_x, 3)
print(torch_xf.size())
torch.Size([3, 32, 17, 2])
Problem
I'm trying to build a text classifier network using LSTM. The error I'm getting is:
RuntimeError: Expected hidden[0] size (4, 600, 256), got (4, 64, 256)
Details
The data is json and looks like this:
{"cat": "music", "desc": "I'm in love with the song's intro!", "sent": "h"}
I'm using torchtext to load the data.
from torchtext import data
from torchtext import datasets
TEXT = data.Field(fix_length = 600)
LABEL = data.Field(fix_length = 10)
BATCH_SIZE = 64
fields = {
'cat': ('c', LABEL),
'desc': ('d', TEXT),
'sent': ('s', LABEL),
}
My LSTM looks like this
EMBEDDING_DIM = 64
HIDDEN_DIM = 256
N_LAYERS = 4
MyLSTM(
(embedding): Embedding(11967, 64)
(lstm): LSTM(64, 256, num_layers=4, batch_first=True, dropout=0.5)
(dropout): Dropout(p=0.3, inplace=False)
(fc): Linear(in_features=256, out_features=8, bias=True)
(sig): Sigmoid()
)
I end up with the following dimensions for the inputs and labels
batch = list(train_iterator)[0]
inputs, labels = batch
print(inputs.shape) # torch.Size([600, 64])
print(labels.shape) # torch.Size([100, 2, 64])
And my initialized hidden tensor looks like:
hidden # [torch.Size([4, 64, 256]), torch.Size([4, 64, 256])]
Question
I'm trying to understand what the dimensions at each step should be.
Should the hidden dimension be initialized to (4, 600, 256) or (4, 64, 256)?
The documentation of nn.LSTM - Inputs explains what the dimensions are:
h_0 of shape (num_layers * num_directions, batch, hidden_size): tensor containing the initial hidden state for each element in the batch. If the LSTM is bidirectional, num_directions should be 2, else it should be 1.
Therefore, your hidden state should have size (4, 64, 256), so you did that correctly. On the other hand, you are not providing the correct size for the input.
input of shape (seq_len, batch, input_size): tensor containing the features of the input sequence. The input can also be a packed variable length sequence. See torch.nn.utils.rnn.pack_padded_sequence() or torch.nn.utils.rnn.pack_sequence() for details.
While it says that the size of the input needs to be (seq_len, batch, input_size), you've set batch_first=True in your LSTM, which swaps batch and seq_len. Therefore your input should have size (batch_size, seq_len, input_size), but that is not the case as your input has seq_len first (600) and batch second (64), which is the default in torchtext because that's the more common representation, which also matches the default behaviour of LSTM.
You need to set batch_first=False in your LSTM.
Alternatively. if you prefer having batch as the first dimension in general, torch.data.Field also has the batch_first option.
I have a model fitted with data but having trouble using the predict function.
d = {'df_Size': [1, 3, 5, 8, 10, 15, 18], 'RAM': [3676, 6532, 9432, 13697, 16633, 23620, 27990]}
df = pd.DataFrame(data=d)
df
X = np.array(df['df_Size']).reshape(-1, 1)
y = np.array(df['RAM']).reshape(-1, 1)
model = LinearRegression()
model.fit(X, y)
print(regr.score(X, y))
then when I try to predict on
X_Size = 25
X_Size
prediction = model.predict(X_Size)
I get the following error
ValueError: Expected 2D array, got scalar array instead:
array=25.
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
I think I am passing the 25 in the wrong format but would just like some help on getting the response for Ram considering the 25 rows.
Thanks,
You need to pass the predictor in the same shape (basically 1 column):
X.shape
Out[11]: (7, 1)
You can do:
model.predict(np.array(25).reshape(1,1))
My data is a (83,104) pandas dataframe. And my first(only) hidden layer is expected to have 5 neurons.
When I wrote my code like this:
input_img = Input(shape=(104,)) # 104 dates as variable
encoded = Dense(5, activation='relu')(input_img)
decoded = Dense(104, activation='relu')(encoded)
autoencoder = Model(input_img, decoded)
autoencoder.compile(loss='mean_squared_error', optimizer='sgd')
autoencoder.fit(train_data, train_data, epochs=50)
I receive error message:
ValueError: Shape mismatch: x has 32 cols (and 83 rows) but y has 104 rows (and 5 cols)
Apply node that caused the error: Dot22(/input_24, dense_47/kernel)
Toposort index: 2
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(83, 32), (104, 5)]
Inputs strides: [(4, 332), (20, 4)]
I have followed the guidance from https://blog.keras.io/building-autoencoders-in-keras.html.
Does anyone know what is wrong here?
Thanks,
Hi I'm a beginner keras.
I'm making some model.
step 1. Input batch and word list, (BATCH_SIZE, WORD_INDEX_LIST)
step 2. Get word embeddings each words (BATCH_SIZE, WORD_LENGTH, EMBEDDING_SIZE)
step 3. Average each each word embeddings in each batch. (BATCH_SIZE, EMBEDDING_SIZE)
step 4. Repeat vector N, (BATCH_SIZE, N, EMBEDDING_SIZE)
step 5. Apply Dense Layer each time step
So, I write code.
MAX_LEN = 20 ( = WORD_INDEX_LIST)
step 1
layer_target_input = Input(shape=(MAX_LEN,), dtype="int32", name="layer_target_input")
# step2
layer_embedding = Embedding(input_dim = n_symbols+1, output_dim=vector_dim,input_length=MAX_LEN,
name="embedding", weights= [embedding_weights],trainable = False)
encoded_target = layer_embedding(layer_target_input)
# step 3
encoded_target_agg = KL.core.Lambda( lambda x: K.sum(x, axis=1) )(encoded_target)
#step 4
encoded_target_agg_repeat = KL.RepeatVector( MAX_LEN)(encoded_target_agg)
# step 5
layer_annotated_tahn = KL.Dense(output_dim=50, name="layer_tahn")
layer_annotated_tahn_td = KL.TimeDistributed(layer_annotated_tahn) (encoded_target_agg_repeat)
model = KM.Model(input=[layer_target_input], output=[ layer_annotated_tahn_td])
r = model.predict({ "layer_target_input":dev_targ}) # dev_targ = (2, 20, 300)
But, when i run this code,
result is bellow.
Traceback (most recent call last):
File "Main.py", line 127, in <module>
r = model.predict({ "layer_target_input":dev_targ})
File "/usr/local/anaconda/lib/python2.7/site-packages/Keras-1.0.7-py2.7.egg/keras/engine/training.py", line 1180, in predict
batch_size=batch_size, verbose=verbose)
File "/usr/local/anaconda/lib/python2.7/site-packages/Keras-1.0.7-py2.7.egg/keras/engine/training.py", line 888, in _predict_loop
outs[i][batch_start:batch_end] = batch_out
ValueError: could not broadcast input array from shape (30,20,50) into shape (2,20,50)
why batch size is changed?
What I have wrong?
The problem is in Lambda operator. In your case it takes a tensor of shape (batch_size, max_len, embedding_size) and is expected to produce a tensor of shape (batch_size, embedding_size). However, the Lambda op doesn't know what transformation you apply internally, and therefore during the graph compilation mistakenly assumes that the shape doesn't change, therefore assuming that the output shape is (batch_size, max_len, embedding_size). The RepeastVector that follows expects the input to be two-dimensional, but never asserts that it is the case. The way it produces the expected shape is (batch_size, num_repetitions, in_shape[1]). Since Lambda mistakenly reported its shape as (batch_size, max_len, embedding_size), RepeatVector now reports its shape as (batch_size, num_repetitions, max_len) instead of expected (batch_size, num_repetitions, embedding_size). num_repetitions in your case is the same as max_len, so RepeastVector reports its shape as (batch_size, max_len, max_len). The way TimeDistributed(Dense) works is:
Reshape((-1, input_shape[2]))
Dense()
Reshape((-1, input_shape[1], num_outputs))
By now input_shape[2] is mistakenly assumed to be max_len instead of embedding_size, but the actual tensor that is given has correct shape of (batch_size, max_len, embedding_size), so what ends up happening is:
Reshape((batch_size * embedding_size, max_len))
Dense()
Reshape((batch_size * embedding_size / max_len, max_len, num_outputs))
In your case batch_size * embedding_size / max_len happens to be 2 * 300 / 20 = 30, that's where your wrong shape comes from.
To fix it, you need to explicitly tell Lambda the shape you want it to produce:
encoded_target_agg = KL.core.Lambda( lambda x: K.sum(x, axis=1), output_shape=(vector_dim,))(encoded_target)