Operands could not be broadcast together with remapped shapes - python

1 np.random.seed(1)
2 x = np.random.randn(4,3,3,2)
--> 3 x_pad = zero_pad(x, 2)
4
5 print("x.shape =", x.shape)
operands could not be broadcast together with remapped shapes [original->remapped]: (3,2) and requested shape (4,2)
I got this error when I wanted to run the codes written in an online course myself. I could not understand what caused the error. I would be glad if you help.

Related

ValueError: Operands could not be broadcast together

I'm multiplying two matrix of shape (3,2,2,2) and shape (2,2,2,2) which as far as I understand should multiply correctly.
np.random.randn(3,2,2,2)#np.random.randn(2,2,2,2)
Raises the error
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (3,2,2,2)->(3,2,newaxis,newaxis) (2,2,2,2)->(2,2,newaxis,newaxis) and requested shape (2,2)
Seeing it in the context of 3x2 and 2x2 matrix with each element as 2x2, the matrix multiplication should work correctly, but doesn't. Looking for correction here.
Edit: using np.dot(np.random.randn(3,2,2,2),np.random.randn(2,2,2,2))
does result in a valid multiplication however, the resultant shape is (3,2,2,2,2,2) which is not expected. Following conventional rules the output shape should be (3,2,2,2).

Can't transform list of arrays into 3D array

I have 3 classifiers that run over 288 samples. All of them are sklearn.neural_network.MLPClassifier structures. Here is the code i am using:
list_of_clfs = [MLPClassifier(...), MLPClassifier(...), MLPClassifier(...)]
probas_list = []
for clf in list_of_clfs:
probas_list.append(clf.predict_proba(X_test))
Each predict_proba(X_test) will return a 2D array with shape (n_samples, n_classes). Then, i am creating a 3D array that will contain all predict_proba() in one single place:
proba = np.array(probas_list) #this should return a (3, n_samples, n_classes) array
This should work fine, but i get an error:
ValueError: could not broadcast input array from shape (288,4) into shape (288)
I don't know why, but this works with dummy examples but not with my dataset.
update: it seems like one of the predict_proba() calls is returning an array of shape (288, 2) but my problem has 4 classes. All classifiers are being tested on the same dataset, so i don't know what this comes from.
Thanks in advance

Adding a constant on probit models in Python

I'm using the statsmodel package to run a probit model, problem is that at 0 the model always predicts 0.5. I followed directions I found online to add a constant array into my input data using statsmodels.api.add_constant(), but then doing model.fit().predict() returns errors, does anybody know what I might be doing wrong?
x is a vector containing my input data and y is my response vector.
I also use sm for statsmodels.api and p for probit
X=sm.add_constant(x)
model=p(y,X).fit()
I then run model.predict(sm.add_constant(.5)) and I get
ValueError: shapes (1,1) and (2,) not aligned: 1 (dim 1) != 2 (dim 0)

ValueError with attention Dimension1 in both shapes must be equal

Hello together I have a problem
I am using python 3.6.5 and tensorflow 1.8.0.
My input is 1000 max_textlength * 64 embedding * 4 steps and 3 protocolls = 64007
neuronumber = 10
a normal RNN works but I wanted to improve it with
attentioncellwrapper(neurons, 2, state_is_tuple = True)
I received the following message:
Value Error: Dimension 1 in both shapes must be equal, but are 10 and
20. Shapes are [?, 10) and [?, 20].
From merging shape 1 with another shape for 'fully_connected/packed'(op: Pack) with input shapes [?,10], [?,10], [?,20]
why is this so?
Has anyone also had this problem?
I was also experimenting with state_is_tuple = False,
no error message was given, but python suddenly crashed down :(
By the way, when I was changing the attention_length the e.g. from 2 to 3 or 4 this changed to
Value Error: Dimension 1 in both shapes must be equal, but are 10 and
30. Shapes are [?, 10) and [?, 30].
Value Error: Dimension 1 in both shapes must be equal, but are 10 and
40. Shapes are [?, 10) and [?, 40].
seems as if the attention length is multiplicated with the shape
Thanks so much for help!

Conv1D confusion in Tensorflow

Trying to implement a paper and running into some brick-walls due to some dimensionality problems. My input is mono audio data where 128 frames of 50ms of 16kHz sampled audio is fed into the network. So my input shape is:
[128,0.005*16000, 1]
Here's the layer details -
1.) conv-bank block : Conv1d-bank-8, LeReLU, IN (instance normalization)
I achieve this using :
bank_width = 8
conv_bank_outputs = tf.concat([ tf.layers.conv1d(input,1,k,activation=tf.nn.leaky_relu,padding="same") for k in range(1, bank_width + 1)], axes = -1)
2.) conv-block: C-512-5, LReLu --> C-512-5,stride=2, LReLu, IN, RES (Residual)
This is where I get stuck, the shapes of the output of second convolution and input to the (2) layer is mismatched. I can't get my head around it.
I achieve this using:
block_1 = tf.layers.conv1d(input,filters=512,kernel_size=5,activation=tf.nn.leaky_relu,padding="same")
block_2 = tf.layers.conv1d(block_1,filters=512,kernel_size=5,strides=2,activation=tf.nn.leaky_relu,padding="same")
IN = tf.contrib.layers.instance_norm(block_2)
RES = IN + input
Error: ValueError: Dimensions must be equal, but are 400 and 800 for 'add' (op: 'Add') with input shapes: [128,400,512], [128,800,1024].
When you run conv1d on block1 with stride = 2 , input data is halved as conv1d effectively samples only alternate numbers and also you have changed number of channels. This is usually worked around by downsampling input by 1x1 conv with stride 2 and filters 512, though I can be more specific if you can share the paper.

Categories

Resources