0
votes

I am currently doing a grade 11 school project on neural networks. I have managed to create one with keras but I have no idea what to do after training. My big question is, how do I input a new data set with the same parameters, same weights, same everything for the training data, but with a whole new set of numbers. Here's my code so far:

from keras.models import Sequential
from keras.layers import Dense
import numpy

seed = 6
numpy.random.seed(seed)

dataset = numpy.loadtxt("Neural_Network_Dataset.csv", delimiter=",")

X = dataset[:,0:6]
Y = dataset[:,11]

model = Sequential()
model.add(Dense(20, input_dim=6, init='uniform', activation='softmax'))
model.add(Dense(20, init='uniform', activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='Adam', metrics=['accuracy'])

model.fit(X, Y, epochs=100, batch_size=100,  verbose=2)


predictions = model.predict(X)
rounded = [round(x[0]) for x in predictions]
for a in range (len(rounded)):
    print (rounded[a])

#print(rounded)
print(predictions)

Test = str(input("Please enter the file name + file type"))
dataset = numpy.loadtxt(Test, delimiter=",")
w = dataset[:,0:6]
v = dataset[:,11]

model.fit(w, v, epochs=1, verbose=2)


predictions = model.predict(w)
rounded = [round(w[0]) for w in predictions]
print (rounded[a])

The help would be greatly appreciated!

2

2 Answers

0
votes

So, the idea behind splitting data into training and testing is to be to able to see if your model is cheating. Has it memorized everything it saw or can it answer a question that was not in the examples you gave it.

Thus, the examples are the training set where questions have solutions and you correct it every time it makes a mistake. That is training. Or as per your code - the model.fit().

Once you have taught it was things should look like, no training required now. It's time to test. So, don't model.fit(w, v,..). Just predict. And check with the answers it produced. That is the out of sample performance or testing accuracy. Now you know how it does on things it hasn't seen in the past.

Hope that helps.

0
votes

So if i understand your question correctly you want to load the weights of already trained network and train new data set or new set of images.

If that's the case then you will have to first use the ModelCheckpoint Callback in keras to save model after every epoch.

from keras.callbacks import ModelCheckpoint
filepath = "model.h5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
model.fit(X, Y, epochs=100, batch_size=100,  verbose=2, callbacks=callbacks_list)

This call back will save your model weights and you can load them while prediction or while training on new dataset

And the question of parameters , you will have to set them manually every time. For instance all the parameters during compile time you will have to set them.

As the weights file is result of all the parameters you set for model training.