简体   繁体   中英

Predictive model using Keras and tensorflow

So I have created a predictive model using Keras, which has accuracy about 60%-65%.

So the data we pass is xtst , xtrn , ytst , ytrn to train_test_split , test_split=.3 and so on, to train and test on supervised data. Now after all these I have a new set of data say xnew .

How do i use this data to predict the y values for this new data?

Where should i feed this xnew data for it to give me y ?

The model:

model = Sequential() 
model.add(Dense(10,input_shape=(4,),activation = 'relu') 
model.add(Dense(32,activation = 'relu')) 
model.add(Dense(101,activation = 'softmax')) 

from keras import optimizers 
model.compile(Adam(lr=.01),loss='sparse_categorical_crossentropy',metrics=['accuracy']) 
model.fit(x_train,y_train,batch_size=20,epochs=40,shuffle=True,verbose=2) 
pred = model.predict(x_test,batch_size = 10,verbose = 2) 
for i in pred: 
  print(i)

When you have trained your model you can use model.save(your_model_name.h5) to save your model. Then you can load it again using model = load_model(your_model_name.h5) . From there you can use model.predict(xnew) or perhaps model.predict_classes(xnew) if you have made a classifier. I suggest that you look at the Model API also.

for a multi-category classifier model with a softmax output layer you can train the model using those vectors

x => [....]             #some vector
y => [0,0,0,1,0,0,0,...] 

where y is a vector indicating the probabilities of each category

to predict given some x like y = model.predict(x) you will get a probabilities vector like this [0.1, 0.05, 0.5, ....] you simply need to find the index with the max probability, you can use category = numpy.argmax(y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM