Why did this Keras python program fail?
I followed a tutorial on youtube and I accidentally didn't add
model.add(Dense(6, activation='relu')) on Keras and I got 36% accuracy. After I added this code it rised to 86%. Why did this happen?
This is the code
from sklearn.model_selection import train_test_split import keras from keras.models import Sequential from keras.layers import Dense import numpy as np np.random.seed(3) classifications = 3 dataset = np.loadtxt('wine.csv', delimiter=",") X = dataset[:,1:14] Y = dataset[:,0:1] x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.66, random_state=5) y_train = keras.utils.to_categorical(y_train-1, classifications) y_test = keras.utils.to_categorical(y_test-1, classifications) model = Sequential() model.add(Dense(10, input_dim=13, activation='relu')) model.add(Dense(8, activation='relu')) model.add(Dense(6, activation='relu')) # This is the code I missed model.add(Dense(6, activation='relu')) model.add(Dense(4, activation='relu')) model.add(Dense(2, activation='relu')) model.add(Dense(classifications, activation='softmax')) model.compile(loss="categorical_crossentropy", optimizer="adam", metrics= ['accuracy']) model.fit(x_train, y_train, batch_size=15, epochs=2500, validation_data= (x_test, y_test))
In my opinion, maybe it's the ratio of your training set to your test set. You have 66% of your test set, so it's possible that training with this model will be under fitting. So one less layer of dense will have a greater change in the accuracy . You put
test_size = 0.2and try again the change in the accuracy of the missing layer.
Number of layers is an hyper parameter just like learning rate,no of neurons. These play an important role in determining the accuracy. So in your case.
This layer played the key roll. We cannot understand what exactly these layers are actually doing. The best we can do is to do hyper parameter tuning to get the best combination of hyper parameters.
See also questions close to this topic
Comparing list values and storing new ones in a separate list
import csv with open("DADSA RESIT CWK JULY 2018.csv", newline='') as f: r = csv.reader(f) database = list(r) del database names =  names.append() def fillnames(d, n): for j in n: for i in d: if d[i] == n[j] and d[i] == n[j]: n[i] = n[i]+1 else: names.append([d[i], d[i], 0]) fillnames(database, names) for i in names: print(i)
The code I have here is me scanning in a csv file into a list. I then want to count how many entries share the same name, by scanning each new name into a separate list, then incrementing the number found every time I find a new one. Every time I run this code it returns "TypeError: list indices must be integers or slices, not list."
Changing font of a list python
Say I have some code that makes a list into a 4 by 4 1d array:
nlist = [2,2,4,8, 0,0,0,0, 0,0,0,0, 0,0,0,0] def drawBoard(): count = 0 for i in range(16): print(nlist[i], end = ' ') count += 1 if count == 4: print("") count = 0 print("") drawBoard()
How can I change all the fonts in this list into size 26. I tried doing font = 'times 26' but I don't know where to put it or if that command needs tkinter.
Get mouse coordinates without clicking in matplotlib
In a matplotlib plot, how can I continuously read the coordinates of the mouse when it is moved, but without waiting for clicks? This is possible in matlab, and there is a mpld3 plugin to do almost exactly what I want, but I can't see how to actually access the coordinates from it. There is also the package mpldatacursor, but this seems to require clicks. Searching for things like "matplotlib mouse coordinates without clicking" did not yield answers.
Answers using additional packages such as mpld3 are fine, but it seems like a pure matplotlib solution should be possible.
Concatenating vectors for CNN in Keras/tensorflow
I am trying to concatenate the flattened output of a CNN, and a vector of scalar values. I am trying to influence the network by both the image and the vector. So the output of the flattened CNN size is
(1,1024), and the vector I want to concatenate is
Of course keras wants them to be similar sizes. So what is the best practice in this situation? Expand the vector to
1024with zeroes? I am trying to make the vector have an impact, what other options can I do? I am trying to do something similar to here (https://arxiv.org/abs/1603.02199).
'tflite_convert' is not recognized as an internal or external command (in windows)
im trying to convert my saved_model.pb(from object detection API) file to .tflite for mlkit but when i execute the command on cmd:
tflite_convert \ --output_file=/saved_model/maonani.tflite \ --saved_model_dir=/saved_model/saved_model
i get a response saying
C:\Users\LENOVO-PC\tensorflow> tflite_convert \ --output_file=/saved_model/maonani.tflite \ --saved_model_dir=/saved_model/saved_model 'tflite_convert' is not recognized as an internal or external command, operable program or batch file.
what should i do to make this work?
Reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero
I am having some issues with my code. I want to build a multi-class (4 Classes) Image classifier using Keras with Tensorflow as a backend, I have written some code, but I keep getting the following error : "Reshape cannot infer the missing input size for an empty tensor unless all specified input sizes are non-zero" at the line below: model.fit_generator(train_generator, samples_per_epoch=samples_per_epoch, epochs=epochs, validation_data=validation_generator, callbacks=cbks, validation_steps=validation_steps)
below is my code base
from __future__ import print_function from keras.engine.input_layer import InputLayer import numpy as np import sys import os import argparse import keras from keras import optimizers from keras import callbacks from keras import backend as K from keras.models import Model from keras.models import Sequential from keras.layers import Input from keras.layers import Lambda from keras.layers import Layer from keras.layers import Dense from keras.layers import Dropout from keras.layers import Activation from keras.layers import Flatten from keras.layers import Conv2D from keras.layers import MaxPooling2D from keras.optimizers import SGD from keras.optimizers import RMSprop from keras.preprocessing.image import ImageDataGenerator epochs = 20 train_data_path = os.path.join(os.getcwd(),"Scans","Train") validation_data_path = train_data_path ##### Parameters ##### img_width, img_height = 150, 150 batch_size = 32 samples_per_epoch = 1000 validation_steps = 300 nb_filters1 = 8 #16 #8 #4 nb_filters2 = 16 #32 #16 #8 conv1_size = 8 conv2_size = 8 pool_size = 2 classes_num = 4 lr = 0.0004 strides = (2,2) ################# Main ################## model = Sequential() inputshape = (img_width,img_height,3) model.add(InputLayer(input_shape=inputshape)) model.add(Conv2D(filters=nb_filters1, kernel_size=conv1_size,strides=1,padding="same")) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(pool_size,pool_size), strides=(2,2),padding="same")) model.add(Conv2D(filters=nb_filters2, kernel_size=conv2_size,padding="same")) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(pool_size,pool_size), strides=(2,2),padding="same")) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation("relu")) model.add(Dropout(0.5)) model.add(Dense(classes_num)) model.add(Activation("softmax")) model.compile(loss='categorical_crossentropy', optimizer=optimizers.RMSprop(lr=lr), metrics=['accuracy']) train_datagen = ImageDataGenerator(rescale=1. /255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. /255) train_generator = train_datagen.flow_from_directory(train_data_path, target_size=(img_width,img_height), batch_size=batch_size, class_mode='categorical') validation_generator = test_datagen.flow_from_directory(validation_data_path, target_size=(img_width,img_height), batch_size=batch_size, class_mode='categorical') ######### TensorBoard Log ############### log_dir = os.path.join(os.getcwd(), "tensorflow_log") tb_cb = callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1) cbks = [tb_cb] model.fit_generator(train_generator, samples_per_epoch=samples_per_epoch, epochs=epochs, validation_data=validation_generator, callbacks=cbks, validation_steps=validation_steps) model.summary() target_dir = os.path.join(os.getcwd(),"models") if not os.path.exists(target_dir): os.mkdir(target_dir) model.save(os.path.join(target_dir,"model.h5")) model.save_weights(os.path.join(target_dir, "weights.h5"))
image segmentation on retinal blood vessels in matlab
For classification, there are different supervised methods. Which method is better for retinal blood vessels segmentation from color images? CNN or Random Forest Classifier(RFC).
How to create bi-modal initial weight distribution in tensorflow?
How can I create a bi-modal initial weight distribution in tensorflow?
I want to create a distribution that is composed of 2 normal distributions centered at 0.15 and -0.15 each with stds of 0.1.
Recommender metrics from embedding
I built a deep collaborative filtering through Keras as a predicted rating recommender system, I use cosine similarity on embedding to generate recommendation and I would like to know how to compute an information retrieval metrics likes recall at k or accuracy at k or any metrics that fit well to evaluate my recommender system.
Keras not train on categorical data LSTM
My dataframe has 5 features and 1 target. I am using Many-to-One model and
LSTMlayers to predict(classify) the target. Target has three category.
My dataframe look like:
Lupper Lbody Llower OStyle CStyle Var Color 0 equal short equal open_equal_high close_high larg_inc red 1 equal short equal open_equal_high close_equal ext_dec green 2 equal equal equal open_equal_high close_equal_high sm_inc red 3 equal equal short open_high close_equal_high norm_dec green 4 equal equal equal open_equal_high close_equal_high sm_dec green 5 equal short equal open_equal_low close_equal_low ext_dec green 6 equal equal equal open_equal close_equal ext_dec green 7 equal equal equal open_equal_high close_equal sm_dec green 8 short short equal open_equal_low close_equal_low sm_inc red 9 short short equal open_equal_low close_equal ext_dec green
I replace my
intnumbers for classifying:
df['Color'].replace('green',1,inplace=True) df['Color'].replace('red',2,inplace=True) df['Color'].replace('cross',0,inplace=True)
And after that, I am creating dummy variable of features and creating time-series window. My time-series window is an overlapping window(0,1,2,3,4),(1,2,3,4,5), ...:
cols_to_transform = ['Lupper','Lbody','Llower','OStyle','CStyle','Var'] df = pd.get_dummies(df,columns=cols_to_transform) def create_dataset(dataset, look_back=1): dataX, dataY = ,  for i in range(len(dataset)-look_back-1): a = dataset[i:(i + look_back), 1:29] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return np.array(dataX), np.array(dataY) X, Y = create_dataset(df.values,look_back=5) Y = to_categorical(Y,num_classes=3) model = Sequential() model.add(LSTM(64,input_shape=(5,28),return_sequences=True)) model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(LSTM(64,return_sequences=True)) model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(LSTM(32)) model.add(Dropout(0.2)) model.add(Dense(3)) model.add(Activation('softmax')) model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['acc']) model.fit(X,Y,batch_size=128,epochs=50,validation_split=0.2)
accimprove from 40% to 50% but
val_accstuck at 41%-42%. Also I try it via
SVM, it's accuracy was 41%-42% .
What's the problem with the model? Am I creating time-series window correctly?
How to sentence Tokenize a list of Paragraphs in Python?
I am learning word2vec technique at the moment and got stuck at sentence tokenizing my text data. Hopefully, someone can help me work out how to do it properly.
So, my data is a bunch of complaint notes of our customers. When I loaded my data into a python list, it became something like:
text = ['this is the first sentence of the first paragraph. and this is the second sentence.','some random text in the second paragraph. and another test sentence.','here is the third paragraph. and this is another sentence','I have run out of text here. I am learning python and deep learning.','another paragraph with some random text. the this is a learning sample.','I need help implementing word2vec. this all sounds exciting.','it''s sunday and I shoudnt be learning in the first place. it''s nice and sunny here.']
I tried some of the most frequently used Sentence Tokenizer methods in the community which all return this error:
TypeError: expected string or bytes-like object
Eventually, I found this:
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle') sentences = tokenizer.tokenize(text[:5]) sentences
This sort of works but I can't work out what index to put in the s e.g. :5 & 4 to get the entire dataset (all the paragraphs) back tokenized as sentences.
Sorry if my question is vague, please ask if you need clarifications.
Could use help formatting data correctly for a Keras SimpleRNN
I'm struggling a bit getting data into the right format for a simpleRNN, or I'm struggling to define the model correctly. I'm hoping someone can spot the problem?
I'm trying to do classification of a list
Xof vectors of length 278 that contain integer values chosen from a dictionary
vocabof length 9026 features as either belonging to class 0 or 1. Here's an example of my input data:
So for example
len(y)=1000My model is:
model.add(L.InputLayer([None],dtype='int32')) model.add(L.Embedding(input_dim=len(vocab)+1,\ output_dim=64,\ input_length=278)) model.add(L.SimpleRNN(64,return_sequences=True)) model.add(L.TimeDistributed(L.Dense(1,activation='softmax'))) model.compile(optimizer='adam',\ loss='categorical_crossentropy',\ metrics=['accuracy'] ) print(model.summary()) _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= embedding_8 (Embedding) (None, 278, 64) 577728 _________________________________________________________________ simple_rnn_7 (SimpleRNN) (None, 278, 64) 8256 _________________________________________________________________ time_distributed_7 (TimeDist (None, 278, 1) 65 ================================================================= Total params: 586,049 Trainable params: 586,049 Non-trainable params: 0 _________________________________________________________________ None
I prepare them as follows:
X=np.array(X) y=keras.utils.to_categorical(y) frac=0.3 random_state=42 X_train,X_tmp,y_train,y_tmp = \ train_test_split(X,y,test_size=frac,random_state=random_state,\ stratify=y) train=(X_train,y_train) test=(X_tmp,y_tmp)
When I run the model:
model.fit(train,train,verbose=0,\ batch_size=batch_size,\ epochs=epochs,validation_data=test)
I get the following error:
ValueError: Error when checking target: expected time_distributed_1 to have 3 dimensions, but got array with shape (5450, 2)
If I change the input data to
and run the model, I get this error:
ValueError: Error when checking target: expected time_distributed_1 to have shape (278, 2) but got array with shape (2, 1)
OK so obviously I've got something wrong since my final dense layer is looking for shape 278 and not 2. So I tried this model without explicitly defining the input_length:
model.add(L.InputLayer([None],dtype='int32')) model.add(L.Embedding(input_dim=len(vocab)+1,\ output_dim=64)) model.add(L.SimpleRNN(64,return_sequences=True)) model.add(L.TimeDistributed(L.Dense(1,activation='softmax'))) model.compile(optimizer='adam',\ loss='categorical_crossentropy',\ metrics=['accuracy'] ) print(model.summary()) _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= embedding_10 (Embedding) (None, None, 64) 577728 _________________________________________________________________ simple_rnn_9 (SimpleRNN) (None, None, 64) 8256 _________________________________________________________________ time_distributed_9 (TimeDist (None, None, 2) 130 ================================================================= Total params: 586,114 Trainable params: 586,114 Non-trainable params: 0
and when I run the model, I get
ValueError: Error when checking target: expected time_distributed_9 to have shape (None, 2) but got array with shape (2, 1)
I'm very confused. Can anyone help me diagnose this?