Keras Reshape: total size of the new array must be unchanged
I'm trying to use Keras Reshape function API to reshape the output of a glove embedding (4D shape: (?, 9, 20, 100)) down to 3D (?, 9, 2000). However, when I tried Reshape((9, 2000))(text_layer), an error pops up and says total size of the new array must be unchanged, even though 9 * 20 * 100 = 9 * 2000. Any ideas why? Code is attached.
text = Input(shape=(9, news_text.shape[1]), name='text')
text_layer = Embedding(
embedding_matrix.shape[0],
embedding_matrix.shape[1],
weights=[embedding_matrix],
input_length=news_text.shape[1]
)(text)
text_layer = Reshape((9, text_layer.shape[2] * text_layer.shape[3]))(text_layer)
1 answer

Remove the
input_length
parameter from theEmbedding
layer.It is strange and I don't know the reason but when you indicate the parameter
input_length
the error is thrown.Anyway, the
Embedding
layer receives the dimension of theInput
layer. It seems that the parameterinput_length
has a very specific use, to know the dimension of the tensor after using aFlatten
layer, etc.In this case, the
Embedding
layer obtains the shape of the output tensor from the input tensor, ignoring theinput_length
parameter.(If you set an invalid value does not throw error until you add the next layer. Note that the
input_lenght
and the resultshape
):>>> inp = Input(shape=(9,20)) >>> emb = Embedding(100,100, input_length=84) (inp) >>> emb <tf.Tensor 'embedding_5/embedding_lookup:0' shape=(?, 9, 20, 100) dtype=float32> >>> res = Reshape((9,2000)) (emb) Traceback (most recent call last): File "<stdin>", line 1, in <module> ...
However, it seems that the
input_length
parameter is in conflict when you add theReshape
layer.Finally:
text = Input(shape=(9, news_text.shape[1]), name='text') text_layer = Embedding( embedding_matrix.shape[0], embedding_matrix.shape[1], weights=[embedding_matrix], )(text) text_layer = Reshape((9, text_layer.shape[2] * text_layer.shape[3]))(text_layer)
See also questions close to this topic

ImportError: cannot import name 'deprecated_endpoints'
from tensorflow.python.util.deprecation import deprecated_endpoints >Import error: cannot import name 'deprecated_endpoints' Could someone help me to resolve this.

How to load two tensorflow frozen models in same python file
I have trained two tf models, the first is to detect car plate, the second is to recognize numbers form the detected plate. Now I want to combine them together into same python file.
I have tried load two models together in a python file, however, there are some mistake, which is the recognition result is really bad compared with using single recognition model, I guess the reason is due two the wrong method to load two frozen graphs, so is there any one can tell me how to load two frozen graphs correctly.
code:
PATH_TO_FROZEN_GRAPH ='assets/plate_model/frozen_inference_graph.pb' # List of the strings that is used to add correct label for eachbox. PATH_TO_LABELS = 'assets/plate_model/label.pbtxt' category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True) detection_graph = tf.Graph().as_default() od_graph_def = tf.GraphDef() fid= tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='detection') sess1 = tf.Session(graph=detection_graph)###detection graph from recognition_v2 import run,recognition sess=run()###recognition graph # Get handles to input and output tensors ops = tf.get_default_graph().get_operations() all_tensor_names = {output.name for op in ops for output in op.outputs} tensor_dict = {} for key in [ 'num_detections', 'detection_boxes', 'detection_scores', 'detection_classes', 'detection_masks' ]: tensor_name = key + ':0' if tensor_name in all_tensor_names: tensor_dict[key] = tf.get_default_graph().get_tensor_by_name( tensor_name) if 'detection_masks' in tensor_dict: # The following processing is only for single image detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0]) detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0]) # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size. real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32) detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, 1]) detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, 1, 1]) detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks( detection_masks, detection_boxes, image.shape[0], image.shape[1]) detection_masks_reframed = tf.cast( tf.greater(detection_masks_reframed, 0.5), tf.uint8) # Follow the convention by adding back the batch dimension tensor_dict['detection_masks'] = tf.expand_dims( detection_masks_reframed, 0) image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0') n=0 for i in [1]: cam = cv2.VideoCapture(f'../../input/ferry_{i}.mp4') # cam.set(cv2.CAP_PROP_FRAME_WIDTH,1920) # cam.set(cv2.CAP_PROP_FRAME_HEIGHT,1080) width = int(cam.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cam.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = cam.get(cv2.CAP_PROP_FPS) fps_time = 0 count = 0 has_plate_cnt=0 cc=0 fps1 = cam.get(cv2.CAP_PROP_FPS) while True: success, image = cam.read() cc+=1 if not success: break image_h = image.shape[0] image_w = image.shape[1] count += 1 if has_plate_cnt > 0: has_plate_cnt = 1 # print(count) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # image = cv2.flip(image, 1) # Run inference output_dict = sess1.run(tensor_dict, feed_dict={image_tensor: np.expand_dims(image, 0)}) # all outputs are float32 numpy arrays, so convert types as appropriate output_dict['num_detections'] = int(output_dict['num_detections'][0]) output_dict['detection_classes'] = output_dict[ 'detection_classes'][0].astype(np.uint8) output_dict['detection_boxes'] = output_dict['detection_boxes'][0] output_dict['detection_scores'] = output_dict['detection_scores'][0] if 'detection_masks' in output_dict: output_dict['detection_masks'] = output_dict['detection_masks'][0] plates = [] p_chars = [] for i, score in enumerate(output_dict['detection_scores']): if score > 0.8: char_name = category_index[output_dict['detection_classes'][i]]['name'] ymin, xmin, ymax, xmax = tuple(output_dict['detection_boxes'][i].tolist()) ymin = int(ymin * image_h) xmin = int(xmin * image_w) ymax = int(ymax * image_h) xmax = int(xmax * image_w) cv2.rectangle(image,(xmin,ymin1),(xmax,ymax+2),(0,255,0),2) if char_name == 'plate': plates.append([ymin, xmin, ymax, xmax]) p_chars.append([]) if has_plate_cnt == 0: plate_image = cv2.cvtColor(image[ymin1:ymax+2,xmin:xmax], cv2.COLOR_BGR2RGB) # shutil.rmtree(f'../../output/data/') # os.mkdir(f'../../output/data/') # cv2.imwrite(f'../../output/data/{init_time}{int(27+cc//fps1)}.jpg', plate_image) cv2.imwrite(f'../../output/data/1.jpg', plate_image) plate_image=cv2.imread(f'../../output/data/1.jpg') recognition(sess,plate_image)########recognition

Different behavior for Batchnorm with is_training 'true' and 'false'  Accuracy is different for frozen models
I am trying to infer mobilenetV2 model.
I have trained the model using tensorflow/models/slim. The model is giving proper accuracy with
is_training=true
. But when I do,is_training=false
and save the model, then inference is giving very less accuracy.I could see the below difference in the graph in these two cases. With
is_training=true
, thenmoving_mean
andmoving_variance
becomeConst
andConst_1
respectively. This is the only difference I could see. And during inference, the output ofFusedBatchNorm
node is different in these two cases.Please, someone, help me to understand why is it happening and how to resolve this issue?

How to pass epoch and batch size when using label powerset in keras
I have a multilabel problem and with some research, I was able to use Label powerset in conjunction with ML algorithms. Now I want to use the Label powerset with neural network and as per the official website I can use Label powerset. But I am not able to understand how to modify my existing code to be able to use Label Powerset.
I want to know how can we pass epoch or batch_size or any other parameter passed in the fit function of the model.
Since I have a multilabel problem I have used MultiLabelBinarizer of sklearn so my each target row looks like this [1,0,0,1,0,0,0,0,0,0,0,0].
and lastly, if someone could explain to me what is KERAS_PARAMS and Keras() in the below line:
def create_model_multiclass(input_dim, output_dim): # create model model = Sequential() model.add(Dense(8, input_dim=input_dim, activation='relu')) model.add(Dense(output_dim, activation='softmax')) # Compile model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model clf = LabelPowerset(classifier=Keras(create_model_multiclass, True, KERAS_PARAMS), require_dense=[True,True]) clf.fit(X_train,y_train) y_pred = clf.predict(X_test)
Below is my existing neural network code
cnn_model = Sequential() cnn_model.add(Dropout(0.5)) cnn_model.add(Conv1D(25,7,activation='relu')) cnn_model.add(MaxPool1D(2)) cnn_model.add(Dropout(0.2)) cnn_model.add(Conv1D(25,7,activation='relu')) cnn_model.add(MaxPool1D(2)) cnn_model.add(Flatten()) cnn_model.add(Dense(25,activation='relu')) cnn_model.add(Dense(12,activation='softmax')) cnn_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc']) history = cnn_model.fit(X_train, y_train, validation_data=(X_test,y_test), batch_size=32, epochs=180,verbose=1) plot_history(history) predictions = cnn_model.predict(X_test)
I want my output row to look like this only [1,0,0,1,0,0,0,0,0,0,0,0] as later I will use my MultiLabelBinarizer for the inverse transform of this.

How to get output of inner layer of nested model correctly?
I'm trying to build SSD on a base of pretrained MobileNetV2. So I need to get output from several layers of MobileNet core, add some Convolutions, priors, Reshaping, Concatenate... And finally, then trying to make a model:
model = Model(inputs=img_input, outputs=out)
, I have a problem with 'Graph disconnection'. Here is my pseudocode:img_input = Input((224,224,3)) conv_model = MobileNetV2(weights='imagenet', include_top=False, alpha=0.35, pooling=None, input_shape=(224, 224, 3)) block14 = conv_model.get_layer('block_14_project_BN').output block14_box_conf = Conv2D(...)(block14) block14_box_loc = Conv2D(...)(block14) block14_priors = PriorBox(...)(block14_box_loc) # same for other blocks # Reshaping, Concatinations... softmax for conf... model = Model(inputs=img_input, outputs=out)
And finally, I have an error:
Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(?, 224, 224, 3), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []
I may build MobileNet "by bricks", but will unable to load weights in this case. And I need to understand the reason of this error.
Also, this error appears if I'm trying to use the output of some inner layer of the nested model, even in not such complicated cases like SSD.

What does max eval parameter in hyperas optim minimize function returns?
I am trying to tune parameters using Hyperas but I can't interpret few details regarding it.
Q1) What is max_eval parameter in optim.minimize do?
Q2) Does it go through each and every combination of parameters for each max_eval and give me best loss based on best of params?
Q3) What if I give max_eval = 5?
Q4) What does best_run and best_model returns after completing all max_evals?
Q5) Below model function I returned loss as test_acc what does it has to do with tuning parameter and why do we use negative sign there?
def model(x_train, y_train, x_test, y_test): dense_units1 = {{choice([64, 126, 256, 512])}} activations = {{choice(['relu', 'sigmoid'])}} epochs = 100 verbose = 0 model = Sequential([ # layer 1 Dense(dense_units1, activations, input_shape=(784,)), .... .... .... ]) # compiling model model.compile(optimizers, loss='categorical_crossentropy', metrics=['accuracy']) # fitting the model result = model.fit(x_train, y_train, validation_split=0.2, batch_size=batch_size, epochs=epochs, verbose=verbose, callbacks=[ES, MC]) test_loss, test_acc = model.evaluate(x_test, y_test, batch_size=512) return {'loss': test_acc, 'status': STATUS_OK, 'model': model} best_run, best_model = optim.minimize(model=model, data=dataset, algo=tpe.suggest, max_evals=5, trials=Trials(), notebook_name='MNIST', verbose=True)

Loss functions reaching global minima
In Deep Learning can we have train accuracy far less than 100% at the global minimum of the loss function?
I have coded a neural network in python to classify cats and noncats. I chose a 2layer network. It gave a train accuracy of 100% and a test accuracy of 70%.
When I increased the #layers to 4 the loss function is getting stuck at 0.6440 leading to train accuracy of 65% and a test accuracy of 34% for many random initializations.
We are expecting that our train accuracy on the 4layer model should be 100%. But we are getting stuck at 65%. We are thinking that the loss function is reaching a global minimum since on many random initialization we are stagnating at a loss value of 0.6440. So, even though the loss function is reaching the global minimum, why is the train accuracy not reaching 100%? Hence our question,"In Deep Learning can we have train accuracy nonzero at the global minimum of the loss function?"

Should I use activation function and normalization for regression?
I have a regression problem, a model in related paper use a minmax normalization to normalize input data and output data to 1 to 1 range, and it apply a tanh activation function in the last output layer. However, I found out it's very hard to train, the loss and rmse decrease slowly. If I remove the activation function in output layer, and do not use any data normalization, it get the best score. So, I have two questions:
Do I have to use some activation function in last layer and some data normalization for a regression problem? (all features and true value are in the same scale, just like the house price in different area etc..)
Even I remove the activation function in last layer, but I found out that if I don't use any data normalization, the loss decrease more faster. If I normalize the data to 1 to 1 range or 0 to 1 range (use minmax normalization), the result is always worse. But, why?

Using RNN and LSTM for process control using available data mesaurements
I have 50 sample measurements of input (temperature) with 600 time steps in each sample. I also have the outputs for these 50 samples in form of 4 variables (like saturation, concentration, lower bound concentration and upper bound concentration).
So the input data is 50 samples, 600 timesteps each and 1 variable or property. The output is 50 samples, 600 timesteps each and 4 variables or properties (and the variable 2 concentration must always be between the variable 3 and 4 which are its lower and upper bounds at all times)
I am new to deep learning and LSTMs. I am having trouble implementing the LSTM. So from what I have read (kindly correct me if I am wrong), I need to use many to many LSTM and batch_size = 50 input_shape is (600,) seq_len = 1
How many units of LSTM to use? I am very confused how to implement LSTM in this.

Extremely poor accuracy and high training time per epoch for a word level language model
Setting up my first wordlevel language model using the Keras API. Extremely poor accuracy results and unbelievably high training time required.
I developed my first wordlevel language model using the Keras library with my training set as the script for Pulp Fiction. I cleaned the text of all punctuation and converted all the words to lower case. When I start training my model on the given dataset it starts off with an accuracy of 3% and a training time of 67 mins per epoch. This is extremely demotivating and I was wondering whether I should tune my hyperparameters or this is normal behaviour for my model and it will yield better results with more epochs?
model=Sequential() model.add(LSTM(256, input_shape=(X.shape[1], X.shape[2]), return_sequences=True)) model.add(Dropout(0.5)) model.add(LSTM(256)) model.add(Dropout(0.5)) model.add(Dense(y.shape[1], activation='softmax')) model.compile(optimizer='adam',loss='categorical_crossentropy', metrics= ['accuracy']) model.fit(X,y,batch_size=128,epochs=100)

how to reshape text data to be suitable for LSTM model in keras
My shape of data is
(87716, 200)
and I want to reshape it in a way I can feed it into LSTM.I have a code for
LSTM Autoencoder
. and the below is the architecture of my mode:inputs = Input(shape=(SEQUENCE_LEN,VOCAB_SIZE), name="input") # inputs = Embedding( VOCAB_SIZE, 256, input_length=SEQUENCE_LEN)(input) encoded = Bidirectional(LSTM(LATENT_SIZE), merge_mode="sum", name="encoder_lstm")(inputs) decoded = RepeatVector(SEQUENCE_LEN, name="repeater")(encoded) decoded = LSTM(VOCAB_SIZE, return_sequences=True)(decoded) autoencoder = Model(inputs, decoded) autoencoder.compile(optimizer="sgd", loss='mse') autoencoder.summary() history = autoencoder.fit(Xtrain, Xtrain,batch_size=BATCH_SIZE, epochs=NUM_EPOCHS)
And this is the way I have prepared the data to feed to model:
BATCH_SIZE=512 VOCAB_SIZE = 4000 SEQUENCE_LEN = 200 sent_wids = np.zeros((len(parsed_sentences),SEQUENCE_LEN),'int32') sample_seq_weights = np.zeros((len(parsed_sentences),SEQUENCE_LEN),'float') for index_sentence in range(len(parsed_sentences)): temp_sentence = parsed_sentences[index_sentence] temp_words = nltk.word_tokenize(temp_sentence) for index_word in range(SEQUENCE_LEN): if index_word < sent_lens[index_sentence]: sent_wids[index_sentence,index_word] = lookup_word2id(temp_words[index_word]) else: sent_wids[index_sentence, index_word] = lookup_word2id('PAD') print(sent_wids.shape) #(87716, 200) Xtrain = sent_wids
Now how can I reshape the data in which I can feed it to this model: I am getting this error :
ValueError: Error when checking input: expected input to have 3 dimensions, but got array with shape (87716, 200)
When I do reshaping like this
Xtrain = Xtrain.reshape(1,SEQUENCE_LEN, VOCAB_SIZE)
.What do we need to do if our prepared data is not dividable by the shapes and batch_size we considered?
Please let me know which part is not clear I will explain.
Thanks for your help:)

could not reshape input array shape question
I tried to reshape the image array
original image shape is (224,224,3)trainImages = np.array([i[0] for i in train_datas]).reshape(1, IMG_SIZE, IMG_SIZE,3)
ValueError: cannot reshape array of size 502562816 into shape (224,224,3)
How should I reshape it when I want to get a (224,224,3)

Row combination to remove NA's based on common ID's
I recently asked a question regarding database managemen in R packages tidyr, dplyr or similar in this link (Reorganizing columns by two column combination).
It was helpful, I managed to do the proposed code, but I was wring in the final format I was requiring.
I cannot manage to reach this format and was wondering how could be easily done.
Given this as an example database:
Factor 1 Factor 2 Year value1 value2 A green 2016 1.9 20 A green 2015 1.9 20 A green 2015 4 30 B yellow 2015 3 10 B yellow 2016 8 11
And trying to obtain:
Factor 1 Factor 2 value1.2015 value1.2016 value2.2015 value2.2016 A green 5.9 1.9 50 20 B yellow 3 8 10 11
So, it would be setting common identifiers for Factor, 1 and Factor 2, to spread the dataset by years and summing up common years for values 1 and 2
I am a begginer with tidyr, dplyr and cannot easily manage to do this.
I have been able to spread the dataset by years doing :
df.spread<df %>% gather(value1,value2,factor1,factor2,Year) %>% unite(Year,Year, value1, sep = "") %>% spread(Year,value2)
but this does not sum the value1 for the common years as I want it.

Go from wide to long format  comma separated columns
I have a data frame that contain multiple comma separated "genes" in column "Symbols". These belong to one category within " Ontology". I would like to split the genes into individual rows, keeping the ontology for each row. Please see my example:
I tried something like this:
reshape(df, direction = "long", varying = list(names(df)[2:2]), v.names = "Symbols", idvar = c("Term"))
in
Term Symbols GO:0003012 TNNI1,STAC3,FKBP1A GO:0003013 BDKRB2,CASQ2,COMP,ENO1
out:
GO:0003012 TNNI1 GO:0003012 STAC3 GO:0003012 FKBP1A GO:0003013 BDKRB2 GO:0003013 CASQ2 GO:0003013 COMP GO:0003013 ENO1