基于keras的二分类的网络训练代码

Data prepare:将liblinear格式的数据,转化成keras下的向量格式的文件


raw_train_data = open("/mnt/software/train_data")
f = file("train_data","w+")
cnt = 0
for t_data in raw_train_data:
    farr = t_data.split(" ")
    y = int(farr[0])
    cnt = cnt+1
    if cnt%10000 == 0:
        print ("progress="+str(cnt))
    features = [0]*78
    if y>0:
        f.write(str(1))
    else:
        f.write(str(0))
    f.write(" ")
    for i in range(1,10):
        kv = farr[i].split(":")
	idx_str = kv[0]
        val = kv[1]
	idx = int(idx_str)
	features[idx] = val
    for feature in features:
        f.write(str(feature))
        f.write(" ")
    f.write("\n")
file.close()
f.close()

训练代码:

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
import numpy as np

model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(78, activation='relu', input_dim=78))
model.add(Dropout(0.5))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])
#x_train = np.random.random((100000, 20))
#y_train = np.random.random((100000, 10))
train_data = np.loadtxt("train_data")
print train_data.shape
y_train = train_data[0:400000,0:1]
x_train = train_data[0:400000,1:79]
model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)
# score = model.evaluate(x_test, y_test, batch_size=128)
model.save("/root/merge_model")


你可能感兴趣的:(Tensorflow,deep,learning)