keras generater

import keras
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
from sklearn.model_selection import train_test_split

count =1    

data=np.array([[1, 2, 0, 1],
               [2, 3, 1, 0],
               [1, 3, 0, 1],
               [1, 4, 0, 1],
               [2, 4, 1, 0],
               [2, 5, 1, 0]])
def generate_arrays_from_file(data):
    global count
    while 1:
        datas =data
        x = datas[:,:2]
        y = datas[:,2:]
        print("count:"+str(count))
#        count = count+1
        yield (x,y)
x_valid = np.array([[1,2],[2,3]])
y_valid = np.array([[0,1],[1,0]])
model = Sequential()
model.add(Dense(units=1000, activation='relu', input_dim=2))
model.add(Dense(units=2, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

model.fit_generator(generate_arrays_from_file(data),steps_per_epoch=10, epochs=2,max_queue_size=1,validation_data=(x_valid, y_valid),workers=1)
# steps_per_epoch 每执行一次steps,就去执行一次生产函数generate_arrays_from_file
# max_queue_size 从生产函数中出来的数据时可以缓存在queue队列中
posted @ 2018-10-29 11:32  luoganttcc  阅读(93)  评论(0编辑  收藏  举报