Found input variables with inconsistent numbers of samples: [3080, 770] - data-science

Below are the x_train and y_train numbers :
x_train.shape --> (296, 770)
y_train.shape --> (3080,)
I tried to transpose the x_train and reshape as well.
x_train.reshape(-1,1)
When i try the fit model I am getting this error.
Below is the error message I get:
Found input variables with inconsistent numbers of samples: [227920, 3080]
any suggestion on how to resolve this issue.

Related

How do I convert a "subset" into slices/tensor/matrix that I obtained using "torch.utils.data.random_split()" in PyTorch?

I'm trying to divide my data into training and testing datasets in PyTorch using torch.utils.data.random_split().
train, test = torch.utils.data.random_split(iris, [112, 38], generator=torch.Generator().manual_seed(42))
The output for the train and set sets from the code above is:
<torch.utils.data.dataset.Subset at 0x141a7379b50>
I'm trying to assign the values below to my training and test sets using the code below, although I'm getting: "TypeError: list indices must be integers or slices, not str" error.
train_X = train[['Sepal.Length', 'Sepal.Width', 'Petal.Length',
'Petal.Width']]
train_y = train.Species
test_X = test[['Sepal.Length', 'Sepal.Width', 'Petal.Length',
'Petal.Width']]
test_y = test.Species
I looked through multiple posts on stackoverflow but couldn't find a proper solution.

np array shape for conv1d input

I have a model with conv1d as the first layer.
My data is time series data where each sample consists of 41 time steps where each time step has 4 features.
I have about 1000 samples.
I have specified the input shape of the conve1d layer to be (41,4) as it supposed to be.
However, I keep getting the following error: Input 0 is incompatible with layer conv1d_48: expected ndim=3, found ndim=2.
I suspect that the problem is that the shape of X is (1000,) while the shape of X[0] is (41,4). Has anyone encountered this problem?
Thanks.
l1=Input(shape=(41,4))
x=Conv1D(64,(4))(l1)
x=GlobalMaxPooling1D()(x)
x=Dense(1)(x)
model=Model(l1,x)
model.compile('rmsprop','binary_crossentropy',metrics=['acc'])
model.fit(X,y,32,10)
You defined an expected input on your Conv1D to be be 2D -> (41, 4)
But you give to it an input of shape (41,), be consistant in your definitions !
If you specify the input_shape in your Conv1D layer, you don't need to feed an Input layer to it.
Or you can change the shape of this Input layer to be consistant with this input_shape.

How to correct shape of Keras input into a 3D array

I've a Keras model that when I fit fails with this error
> kerasInput = Input(shape=(None, 47))
> LSTM(..)(kerasInput)
...
> model.fit(realInput, ...)
ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (10842, 1)
When looking at my input I found it has a shape of (10842, 1) but for each row it's actually a list of list. I can verify with
> pd.DataFrame(realInput[0]).shape
(260, 47)
How I could correct my input shape?
When trying with keras Reshape layer, the creation of the model fails with:
Model inputs must come from `keras.layers.Input` (thus holding past layer metadata), they cannot be the output of a previous non-Input layer. Here, a tensor specified as input to your model was not an Input tensor, it was generated by layer reshape_8.
Note that input tensors are instantiated via `tensor = keras.layers.Input(shape)`.
The tensor that caused the issue was: reshape_8/Reshape:0
You can use numpy.expand_dims method to convert the shape to 3D.
import numpy as np
np.expand_dims(realInput,axis=0)
Reshape layer keras
https://keras.io/layers/core/#reshape
Use the third parameter as 1
# Something Similar to this
X_train = np.reshape(X_train,(X_train.shape[0],X_train.shape[1],1))
Edit: Added np.reshape method
Refer this repository: https://github.com/NilanshBansal/Stock_Price_Prediction/blob/master/Stock_Price_Prediction_20_days_later_4_LSTM.ipynb
As I said before in the comments. You will need to make sure to reshape your data to match what LSTM expects to receive and also make sure the input_shape is correctly set.
I found this post quite helpful when I struggled with inputting to an LSTM layer. I hope it helps you too : Reshape input for LSTM

Moving from pooling to fully connected: logits and labels must have the same first dimension

I'm starting with tensorflow, and I have some problems that I couldn't solve.
I started with some algorithms to input a batch of images to predict them, but I have some problems with shapes.
GIST: https://gist.github.com/anonymous/5140ee46b1036742c26f3b050ec0a7d9
I think the error is on line 53. I'm not sure if n_inputs = 63360 is correct on line 36.
Probably I need to transform batch of images pooled to fully connected layer.
The error is: tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [1,2] and labels shape [2] [[Node:loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits = SparseSoftmaxCrossEntropyWithLogits[T=DT_FLOAT, Tlabels=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](dnn/fully_connected_2/BiasAdd, _arg_Placeholder_1_0_1)]]
But I think that I'm missing some important step.
The problem is caused by mismatched shapes. You can reshape the logits or labels so that their shapes match.
For example:
logits = tf.reshape(logits, [-1])
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits))

Output from TensorFlow `py_func` has unknown rank/shape

I am trying to create a simple neural net in TensorFlow. The only tricky part is I have a custom operation that I have implemented with py_func. When I pass the output from py_func to a Dense layer, TensorFlow complains that the rank should be known. The specific error is:
ValueError: Inputs to `Dense` should have known rank.
I don't know how to preserve the shape of my data when I pass it through py_func. My question is how do I get the correct shape? I have a simple example below to illustrate the problem.
def my_func(x):
return np.sinh(x).astype('float32')
inp = tf.convert_to_tensor(np.arange(5))
y = tf.py_func(my_func, [inp], tf.float32, False)
with tf.Session() as sess:
with sess.as_default():
print(inp.shape)
print(inp.eval())
print(y.shape)
print(y.eval())
The output from this snippet is:
(5,)
[0 1 2 3 4]
<unknown>
[ 0.
1.17520118 3.62686038 10.01787472 27.28991699]
Why is y.shape <unknown>? I want the shape to be (5,) the same as inp. Thanks!
Since py_func can execute arbitrary Python code and output anything, TensorFlow can't figure out the shape (it would require analyzing Python code of function body) You can instead give the shape manually
y.set_shape(inp.get_shape())