Why is my neural network not improving? - python-2.7

I am struggling to understand why i am getting such a high loss/val_loss rate on my training. I am training a regression network. I've normalized the input data to range between -1 to 1, and left the output data unaltered, its range is approx. between -100 to 100.
I chose to normalize the input such that i could use tanh, as activation function as it outputs within this range.
The neural network consist of 3 layers.
print "Model definition!"
model = Sequential()
#act = PReLU(init='normal', weights=None)
model.add(Dense(output_dim=400,input_dim=400, init="normal",activation=K.tanh))
#act1 = PReLU(init='normal', weights=None)
model.add(Dense(output_dim=400,input_dim=400, init="normal",activation=K.tanh))
#act2 = PReLU(init='normal', weights=None)
#model.add(Dense(output_dim=400, input_dim=400, init="normal",activation=K.tanh))
act4=ELU(100)
model.add(Dense(output_dim=13, input_dim=400, init="normal",activation=act4))
The mapping between the input and output consist of mapping audio samples into MFCC features. The samples are the ones i've normalized to the described range.
why am i getting these results?
what could/should i improve/change ?
cv code running:
#Each frame length = 118
#Each feature length = 13
############################### Training setup ##################################
#Define 10 folds:
seed = 7
np.random.seed(seed)
kfold = KFold(n_splits=10, shuffle=False, random_state=None)
print "Splits"
cvscores_acc = []
cvscores_loss = []
hist = []
i = 0
for train, test in kfold.split(train_set_data_vstacked_normalized):
print "Model definition!"
model = Sequential()
#act = PReLU(init='normal', weights=None)
model.add(Dense(output_dim=400,input_dim=400, init="normal",activation=K.tanh))
#act1 = PReLU(init='normal', weights=None)
model.add(Dense(output_dim=400,input_dim=400, init="normal",activation=K.tanh))
#act2 = PReLU(init='normal', weights=None)
#model.add(Dense(output_dim=400, input_dim=400, init="normal",activation=K.tanh))
act4=ELU(100)
model.add(Dense(output_dim=13, input_dim=400, init="normal",activation=act4))
print "Compiling"
model.compile(loss='mean_squared_error', optimizer='RMSprop')
print "Compile done! "
print '\n'
print "Train start"
reduce_lr=ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0.00000001)
log=csv_logger = CSVLogger('training_'+str(i)+'.csv')
hist_current = model.fit(train_set_data_vstacked_normalized[test],train_set_output_vstacked[test], shuffle=False,validation_split=0.1 , nb_epoch=1000,verbose=1,callbacks=[reduce_lr,log])
hist.append(hist_current)
loss, accuracy = model.evaluate(x=train_set_data_vstacked_normalized[train],y=train_set_output_vstacked[train],verbose=1)
print
print('loss: ', loss)
print('accuracy: ', accuracy)
print()
print model.summary()
print "New Model:"
cvscores_acc.append(accuracy)
cvscores_loss.append(loss)
print "Model stored"
model.save("Model"+str(i))
i=i+1
print("%.2f%% (+/- %.2f%%)" % (numpy.mean(cvscores_acc), numpy.std(cvscores_acc)))
print("%.2f%% (+/- %.2f%%)" % (numpy.mean(cvscores_loss), numpy.std(cvscores_loss)))

Related

LSTM classification for 2D dataset

I a dataset with 10000 rows and 4 features (positions of two cars and their velocity) as x_train and also y_train is my labels which is 0 or 1. I want with LSTM classifying my dataset but the accuracy do not go more than 50 percent. For LSTM I tested both input_shape (10000, 4, 1) and (10000, 1, 4) but accuracy remains around 50 percentage. Do you know what should I do to improve the accuracy? and which of (10000, 4, 1) and (10000, 1, 4) are correct format ?
x_train, x_test, y_train, y_test = train_test_split(x, label, test_size=0.25, random_state=42)
x_train = x_train.reshape(-1, 4, 1)
x_test = x_test.reshape(-1, 4, 1)
y_train = y_train.reshape(-1, 1, 1)
y_test = y_test.reshape(-1, 1, 1)
model = Sequential()
model.add(LSTM(256, input_shape=(x_train.shape[1:]), activation='tanh', return_sequences=True))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(LSTM(256, activation='tanh'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Dense(128, activation='tanh'))
model.add(BatchNormalization())
model.add(Dense(2, activation='softmax'))
model.compile( loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
history = model.fit(x_train, y_train, validation_split=0.33, epochs=20, batch_size=128,shuffle=True )

ValueError: Tensor Tensor("Const:0", shape=(), dtype=float32) may not be fed with tf.placeholder

I'm trying to make speech recognition system with tensorflow.
Input data is an numpy array of size 50000 X 1.
Output data (mapping data) is an numpy array of size 400 X 1.
Input and mapping data is passed in batches of 2 in a list.
I've used this tutorial to design the neural network. Following is the code snippet:
For RNN:
input_data = tf.placeholder(tf.float32, [batch_size, sound_constants.MAX_ROW_SIZE_IN_DATA, sound_constants.MAX_COLUMN_SIZE_IN_DATA], name="train_input")
target = tf.placeholder(tf.float32, [batch_size, sound_constants.MAX_ROW_SIZE_IN_TXT, sound_constants.MAX_COLUMN_SIZE_IN_TXT], name="train_output")
fwd_cell = tf.nn.rnn_cell.BasicLSTMCell(num_hidden, state_is_tuple=True, forget_bias=1.0)
# creating one backward cell
bkwd_cell = tf.nn.rnn_cell.BasicLSTMCell(num_hidden, state_is_tuple=True, forget_bias=1.0)
# creating bidirectional RNN
val, _, _ = tf.nn.static_bidirectional_rnn(fwd_cell, bkwd_cell, tf.unstack(input_data), dtype=tf.float32)
For feeding data:
feed = {g['input_data'] : trb[0], g['target'] : trb[1], g['dropout'] : 0.6}
accuracy_, _ = sess.run([g['accuracy'], g['ts']], feed_dict=feed)
accuracy += accuracy_
When I ran the code, I got this error:
Traceback (most recent call last):
File "/home/wolborg/PycharmProjects/speech-to-text-rnn/src/rnn_train_1.py", line 205, in <module>
tr_losses, te_losses = train_network(g)
File "/home/wolborg/PycharmProjects/speech-to-text-rnn/src/rnn_train_1.py", line 177, in train_network
accuracy_, _ = sess.run([g['accuracy'], g['ts']], feed_dict=feed)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run
run_metadata_ptr)
File "/home/wolborg/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1102, in _run
raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Tensor Tensor("Const:0", shape=(), dtype=float32) may not be fed.
Process finished with exit code 1
Earlier, I was facing this issue with tf.sparse_placeholder, then after some browsing, I changed input type to tf.placeholder and made related changes. Now I'm clueless on where I'm making the error.
Please suggest something as how should I feed data.
Entire code:
import tensorflow as tf
# for taking MFCC and label input
import numpy as np
import rnn_input_data_1
import sound_constants
# input constants
# Training Parameters
num_input = 10 # mfcc data input
training_data_size = 8 # determines number of files in training and testing module
testing_data_size = num_input - training_data_size
# Network Parameters
learning_rate = 0.0001 # for large training set, it can be set 0.001
num_hidden = 200 # number of hidden layers
num_classes = 28 # total alphabet classes (a-z) + extra symbols (', ' ')
epoch = 1 # number of iterations
batch_size = 2 # number of batches
mfcc_coeffs, text_data = rnn_input_data_1.mfcc_and_text_encoding()
class DataGenerator:
def __init__(self, data_size):
self.ptr = 0
self.epochs = 0
self.data_size = data_size
def next_batch(self):
self.ptr += batch_size
if self.ptr > self.data_size:
self.epochs += 1
self.ptr = 0
return mfcc_coeffs[self.ptr-batch_size : self.ptr], text_data[self.ptr-batch_size : self.ptr]
def reset_graph():
if 'sess' in globals() and sess:
sess.close()
tf.reset_default_graph()
def struct_network():
print ('Inside struct network !!')
reset_graph()
input_data = tf.placeholder(tf.float32, [batch_size, sound_constants.MAX_ROW_SIZE_IN_DATA, sound_constants.MAX_COLUMN_SIZE_IN_DATA], name="train_input")
target = tf.placeholder(tf.float32, [batch_size, sound_constants.MAX_ROW_SIZE_IN_TXT, sound_constants.MAX_COLUMN_SIZE_IN_TXT], name="train_output")
keep_prob = tf.constant(1.0)
fwd_cell = tf.nn.rnn_cell.BasicLSTMCell(num_hidden, state_is_tuple=True, forget_bias=1.0)
# creating one backward cell
bkwd_cell = tf.nn.rnn_cell.BasicLSTMCell(num_hidden, state_is_tuple=True, forget_bias=1.0)
# creating bidirectional RNN
val, _, _ = tf.nn.static_bidirectional_rnn(fwd_cell, bkwd_cell, tf.unstack(input_data), dtype=tf.float32)
# adding dropouts
val = tf.nn.dropout(val, keep_prob)
val = tf.transpose(val, [1, 0, 2])
last = tf.gather(val, int(val.get_shape()[0]) - 1)
# creating bidirectional RNN
print ('BiRNN created !!')
print ('Last Size: ', last.get_shape())
weight = tf.Variable(tf.truncated_normal([num_hidden * 2, sound_constants.MAX_ROW_SIZE_IN_TXT]))
bias = tf.Variable(tf.constant(0.1, shape=[sound_constants.MAX_ROW_SIZE_IN_TXT]))
# mapping to 28 output classes
logits = tf.matmul(last, weight) + bias
prediction = tf.nn.softmax(logits)
prediction = tf.reshape(prediction, shape = [batch_size, sound_constants.MAX_ROW_SIZE_IN_TXT, sound_constants.MAX_COLUMN_SIZE_IN_TXT])
# getting probability distribution
mat1 = tf.cast(tf.argmax(prediction,1),tf.float32)
correct = tf.equal(prediction, target)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
logits = tf.reshape(logits, shape=[batch_size, sound_constants.MAX_ROW_SIZE_IN_TXT, sound_constants.MAX_COLUMN_SIZE_IN_TXT])
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=target))
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
# returning components as dictionary elements
return {'input_data' : input_data,
'target' : target,
'dropout': keep_prob,
'loss': loss,
'ts': train_step,
'preds': prediction,
'accuracy': accuracy
}
def train_network(graph):
# initialize tensorflow session and all variables
# tf_gpu_config = tf.ConfigProto(allow_soft_placement = True, log_device_placement = True)
# tf_gpu_config.gpu_options.allow_growth = True
# with tf.Session(config = tf_gpu_config) as sess:
with tf.Session() as sess:
train_instance = DataGenerator(training_data_size)
test_instance = DataGenerator(testing_data_size)
print ('Training data size: ', train_instance.data_size)
print ('Testing data size: ', test_instance.data_size)
sess.run(tf.global_variables_initializer())
print ('Starting session...')
step, accuracy = 0, 0
tr_losses, te_losses = [], []
current_epoch = 0
while current_epoch < epoch:
step += 1
trb = train_instance.next_batch()
feed = {g['input_data'] : trb[0], g['target'] : trb[1], g['dropout'] : 0.6}
accuracy_, _ = sess.run([g['accuracy'], g['ts']], feed_dict=feed)
accuracy += accuracy_
if train_instance.epochs > current_epoch:
current_epoch += 1
tr_losses.append(accuracy / step)
step, accuracy = 0, 0
#eval test set
te_epoch = test_instance.epochs
while test_instance.epochs == te_epoch:
step += 1
print ('Testing round ', step)
trc = test_instance.next_batch()
feed = {g['input_data']: trc[0], g['target']: trc[1]}
accuracy_ = sess.run([g['accuracy']], feed_dict=feed)[0]
accuracy += accuracy_
te_losses.append(accuracy / step)
step, accuracy = 0,0
print("Accuracy after epoch", current_epoch, " - tr:", tr_losses[-1], "- te:", te_losses[-1])
return tr_losses, te_losses
g = struct_network()
tr_losses, te_losses = train_network(g)
You defined keep_prob as a tf.constant, but then trying to feed the value into it. Replace keep_prob = tf.constant(1.0) with keep_prob = tf.placeholder(tf.float32,[]) or keep_prob = tf.placeholder_with_default(1.0,[])

Python: Type Error: tuple indices must be integers, not tuple

I am counting number of black pixels to detect motion and getting this error tuple indices must be integers, not tuple please if someone could tell me where i messed up i would be grateful. below is my code.
class MotionDetectorInstantaneous():
def onChange(self, val): #callback when the user change the detection threshold
self.threshold = val
def __init__(self,threshold=15, doRecord=True, showWindows=True):
self.writer = None
self.font = None
self.doRecord=doRecord #Either or not record the moving object
self.show = showWindows #Either or not show the 2 windows
self.frame = None
self.capture = cv2.VideoCapture(0)
self.capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
self.capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 720)
self.width = int(self.capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
self.height = int(self.capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
self.capture.grab()
rval, self.frame = self.capture.retrieve()
if doRecord:
self.initRecorder()
self.frame1gray = np.zeros((self.height,self.width,3), np.uint8)
print "2: frame1gray type: ",type(self.frame1gray) , np.size(self.frame1gray)
self.frame1gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
self.res = np.zeros((self.height,self.width,3), np.uint8)
print "3: res type: ",type(self.res) , np.size(self.res)
self.frame2gray = np.zeros((self.height,self.width,3), np.uint8)
print "4: frame2gray type: ",type(self.frame2gray) , np.size(self.frame2gray)
self.width = self.width
print "width",self.width
self.height = self.height
print "height", self.height
self.nb_pixels = self.width * self.height
self.threshold = threshold
self.isRecording = False
self.trigger_time = 0 #Hold timestamp of the last detection
if showWindows:
cv.NamedWindow("Image")
cv.CreateTrackbar("Detection treshold: ", "Image", self.threshold, 100, self.onChange)
def initRecorder(self): #Create the recorder
codec = cv2.cv.FOURCC('M', 'J', 'P', 'G')
self.writer=cv2.VideoWriter(datetime.now().strftime("%b-%d_%H_%M_%S")+".wmv", codec, 5, (self.width, self.height), 1)
def run(self):
started = time.time()
while True:
self.capture.grab()
rval, curframe = self.capture.retrieve()
print "6: curframe type: ",type(curframe) , np.size(curframe)
instant = time.time() #Get timestamp o the frame
self.processImage(curframe) #Process the image
if not self.isRecording:
if self.somethingHasMoved():
self.trigger_time = instant #Update the trigger_time
if instant > started +5:#Wait 5 second after the webcam start for luminosity adjusting etc..
print datetime.now().strftime("%b %d, %H:%M:%S"), "Something is moving !"
if self.doRecord: #set isRecording=True only if we record a video
self.isRecording = True
else:
if instant >= self.trigger_time +10: #Record during 10 seconds
print datetime.now().strftime("%b %d, %H:%M:%S"), "Stop recording"
self.isRecording = False
else:
#cv.PutText(curframe,datetime.now().strftime("%b %d, %H:%M:%S"), (25,30),self.font, 0) #Put date on the frame
cv.WriteFrame(self.writer, curframe) #Write the frame
if self.show:
cv.ShowImage("Image", curframe)
cv.ShowImage("Res", self.res)
cv.Copy(self.frame2gray, self.frame1gray)
c=cv.WaitKey(1) % 0x100
if c==27 or c == 10: #Break if user enters 'Esc'.
break
def processImage(self, frame):
self.frame2gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
self.res = cv2.absdiff(self.frame1gray, self.frame2gray)
self.res = cv2.GaussianBlur(self.res, (15, 15), 0)
kernel = np.ones((3, 3), np.uint8)
self.res = cv2.morphologyEx(self.res, cv2.MORPH_OPEN, kernel)
self.res = cv2.morphologyEx(self.res, cv2.MORPH_GRADIENT, kernel)
self.res = cv2.threshold(self.res, 10, 255, cv2.THRESH_BINARY_INV)
def somethingHasMoved(self):
nb=0 #Will hold the number of black pixels
for x in range(self.height): #Iterate the hole image
print "row", x
for y in range(self.width):
print "col", y
if self.res[x,y] == 0.0: #If the pixel is black keep it
nb += 1
print "black pixels: ", nb
print "total pixels: ", self.nb_pixels
avg = (nb*100.0)/self.nb_pixels #Calculate the average of black pixel in the image
print"average movement : ",avg
if avg > self.threshold:#If over the ceiling trigger the alarm
return True
else:
return False
if __name__=="__main__":
detect = MotionDetectorInstantaneous(doRecord=True)
detect.run()
In Above code in this fuction def somethingHasMoved(self): i am getting here if self.res[x,y] == 0.0:

Why am i getting poor results?

I am currently working on a Neural network that maps stft audio files to MFCC features..
But keep for some reason keep getting a validation_loss around 200.
Which compared desired range is (-100,100).. so the output the NN outputs is not desirable..
I tried different implementation..
Simple NN 1-layer, 2-layer, 3-layer => wiht no difference.
CNN , 1-layer, 2-layer => same result
tried with random forrest => never finished, memory allocation error.
So...
What am I doing wrong?
I tried normalizing the input and output, but the same error came back when i unormalized the dataset.. so...
the shape of the input is (x,2050) and the shape of the output (x,13)...
Any idea on why I am getting so poor results?..
Normalization:
def numpy_minmax(X):
print X.min()
print X.max()
xmin = X.min()
return (2*(X - xmin) / (X.max() - xmin)-1)*0.9
def numpy_unorm(x):
xmax = 109.2991
xmin = -97.23664
return x*(xmax-xmin)+xmin
files_train_path = [dnn_train+f for f in listdir(dnn_train) if isfile(join(dnn_train, f))]
files_test_path = [dnn_test+f for f in listdir(dnn_test) if isfile(join(dnn_test, f))]
files_train_name = [f for f in listdir(dnn_train) if isfile(join(dnn_train, f))]
files_test_name = [f for f in listdir(dnn_test) if isfile(join(dnn_test, f))]
os.chdir(dnn_train)
#train_name = generate_list_of_names_data(files_train_path)
#print train_name
train_data, train_output_data, max = load_sound_files(files_train_path)
#sys.exit()
#train_data_real, train_data_img = split_real_img(train_data)
#print train_data
train_set_data_vstacked = np.vstack(train_data)
train_set_output_vstacked = np.vstack(train_output_data)
print train_set_data_vstacked.shape
train_set_data_vstacked_normalized = numpy_minmax(train_set_data_vstacked)
train_set_output_vstacked_normalized = numpy_minmax(train_set_output_vstacked)
Currently network structure options tried:
############################### Training setup ##################################
#Define 10 folds:
seed = 7
np.random.seed(seed)
kfold = KFold(n_splits=10, shuffle=False, random_state=None)
print "Splits"
cvscores_acc = []
cvscores_loss = []
hist = []
i = 0
#train_set_data_vstacked_normalized_reshaped = np.reshape(train_set_data_vstacked_normalized,train_set_data_vstacked_normalized.shape+(1,))
#train_set_output_vstacked_normalized_reshaped = np.reshape(train_set_output_vstacked_normalized,train_set_output_vstacked_normalized.shape+(1,))
for train, test in kfold.split(train_set_data_vstacked_normalized):
print "Model definition!"
model = Sequential()
#act = PReLU(init='normal', weights=None)
model.add(Dense(output_dim=2050,input_dim=2050, init="normal", activation='relu'))
#act1 = PReLU(init='normal', weights=None)
#act2 = PReLU(init='normal', weights=None)
model.add(Dense(output_dim=2050, input_dim=2050, init="he_normal",activation='tanh'))
model.add(Dense(output_dim=13, input_dim=2050, init="he_normal",activation='tanh'))
model.add(Lambda(lambda x: numpy_unorm(x)))
#model.add(ELU(100))
#model.add(Convolution1D(13, 3, border_mode='same', input_shape=(2050,1)))
print "Compiling"
#rms_opt = keras.optimizers.RMSprop(lr=0.01, rho=0.9, epsilon=1e-08, decay=0.0)
model.compile(loss='mean_squared_error', optimizer="RMSprop")
print "Compile done! "
print '\n'
print "Train start"
reduce_lr=ReduceLROnPlateau(monitor='val_loss', factor=0.01, patience=3, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0.000000000000000001)
stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')
log=csv_logger = CSVLogger('training_'+str(i)+'.csv')
hist_current = model.fit(train_set_data_vstacked_normalized[train],
train_set_output_vstacked[train],
shuffle=False,
validation_data=(train_set_data_vstacked_normalized[test],train_set_output_vstacked[test]),
validation_split=0.1,
nb_epoch=150,
verbose=1,
callbacks=[reduce_lr,log,stop])
hist.append(hist_current)
Convolutional:
#Each frame length = 118
#Each feature length = 13
############################### Training setup ##################################
#Define 10 folds:
seed = 7
np.random.seed(seed)
kfold = KFold(n_splits=10, shuffle=False, random_state=None)
print "Splits"
cvscores_acc = []
cvscores_loss = []
hist = []
i = 0
#train_set_data_vstacked_normalized_reshaped = np.reshape(train_set_data_vstacked_normalized,train_set_data_vstacked_normalized.shape+(1,))
#train_set_output_vstacked_normalized_reshaped = np.reshape(train_set_output_vstacked_normalized,train_set_output_vstacked_normalized.shape+(1,))
train_set_data_vstacked_reshaped = train_set_data_vstacked[:,newaxis,:]
train_set_output_vstacked_reshaped = train_set_output_vstacked[:,newaxis,:]
print train_set_data_vstacked_reshaped.shape
print train_set_output_vstacked_reshaped.shape
for train, test in kfold.split(train_set_data_vstacked_reshaped):
print "Model definition!"
model = Sequential()
#act = PReLU(init='normal', weights=None)
#model.add(Dense(output_dim=400,input_dim=400, init="normal", activation=K.tanh))
#act1 = PReLU(init='normal', weights=None)
#act2 = PReLU(init='normal', weights=None)
#model.add(Dense(output_dim=2050, input_dim=2050, init="he_normal",activation='tanh'))
#model.add(Dense(output_dim=13, input_dim=2050, init="he_normal",activation='relu'))
#model.add(Lambda(lambda x: numpy_unorm(x)))
#model.add(ELU(100))
model.add(Convolution1D(2050, 1, border_mode='same', input_dim=2050))
model.add(Convolution1D(13, 1, border_mode='same', input_dim=2050))
#model.add(Convolution1D(13, 1, border_mode='same', input_dim=2050))
#model.add(Dense(output_dim=13, input_dim=2050, init="he_normal",activation='relu'))
print "Compiling"
#rms_opt = keras.optimizers.RMSprop(lr=0.01, rho=0.9, epsilon=1e-08, decay=0.0)
model.compile(loss='mean_squared_error', optimizer="Adam")
print "Compile done! "
print '\n'
print "Train start"
reduce_lr=ReduceLROnPlateau(monitor='val_loss', factor=0.01, patience=3, verbose=1, mode='auto', epsilon=0.01, cooldown=0, min_lr=0.000000000000000001)
stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')
log=csv_logger = CSVLogger('training_'+str(i)+'.csv')
hist_current = model.fit(train_set_data_vstacked_reshaped[train],
train_set_output_vstacked_reshaped[train],
shuffle=False,
validation_data=(train_set_data_vstacked_reshaped[test],train_set_output_vstacked_reshaped[test]),
validation_split=0.1,
nb_epoch=150,
verbose=1,
callbacks=[reduce_lr,log,stop])
hist.append(hist_current)

How is the size of each k folds defined?

I am currently training my regression network using crossvalidation, I don't have any labels, but specific input that should be mapped to an specific output, the network should then generate the mapping.I seem to have some problems with how the folds are being defined.
the way i do crossvalidation is like this:
############################### Training setup ##################################
#Define 10 folds:
seed = 7
np.random.seed(seed)
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
print "Splits"
cvscores_loss = []
for train, test in kfold.split(train_set_data_vstacked_normalized,train_set_output_vstacked):
print "Model definition!"
model = Sequential()
#act = PReLU(init='normal', weights=None)
model.add(Dense(output_dim=400,input_dim=400, init="normal",activation=K.tanh))
#act1 = PReLU(init='normal', weights=None)
model.add(Dense(output_dim=400,input_dim=400, init="normal",activation=K.tanh))
#act2 = PReLU(init='normal', weights=None)
model.add(Dense(output_dim=400, input_dim=400, init="normal",activation=K.tanh))
act4=ELU(10000)
model.add(Dense(output_dim=13, input_dim=300, init="normal",activation=act4))
print "Compiling"
model.compile(loss='mean_squared_error', optimizer='RMSprop', metrics=["accuracy"])
print "Compile done! "
print '\n'
print "Train start"
model.fit(train_set_data_vstacked_normalized[train],train_set_output_vstacked[train], nb_epoch=10, verbose=1)
loss, accuracy = model.evaluate(x=train_set_data_vstacked_normalized[test],y=train_set_output_vstacked[test],verbose=1)
print
print('loss: ', loss)
print('accuracy: ', accuracy)
print()
print model.summary()
print "New Model:"
cvscores_loss.append(loss)
print("%.2f%% (+/- %.2f%%)" % (numpy.mean(cvscores_loss), numpy.std(cvscores_loss)))
Problem with this code is that I never enter the for loop.. receive a warning message after "Splits" is printed... It being .
Splits
/home/k/.local/lib/python2.7/site-packages/sklearn/model_selection/_split.py:579: Warning: The least populated class in y has only 1 members, which is too few. The minimum number of groups for any class cannot be less than n_splits=10.
Which make it question how kfold, knows what the input and output dimensions is of my neural network?...
Should i define it somewhere? or how?..
The message tells you the problem. One of your target classes has only 1 member. When it stratifies into 10 folds it needs at least 10 members of each class so that it can put 1 in each fold.
You need to check the counts of the target classes to find the offending class and remove it.
I think you over complicates this. If you need to do cross-validation on Keras model, you can use keras scikit-learn API. To do this you need to:
Import some stuff:
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
create a function that defines your model:
def model_creation():
model = Sequential()
model.add(...)
...
model.compile(...)
return model
and use the wrapper:
model = KerasClassifier(build_fn=model_creation, nb_epoch=100, batch_size=100, verbose=0)
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)
results = cross_val_score(model, X, y, cv=kfold)