Keras__softmax

https://blog.csdn.net/u013378306/article/details/64923361    粗略讲了softmax代码意义

https://blog.csdn.net/qq_35082030/article/details/77170284   详细

下面是在我数据集上实现的代码

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

# Generate dummy data
import numpy as np
import codecs

f = codecs.open("xlj_vec.txt", 'r', 'utf-8')  # codecs包指定TXT打开方式

lines = f.readlines()

X = []
Y = []

zero = []
for i in range(0, 64):
    zero.append(0)
print zero
for line in lines:
    z = line.split(" ", 1)[1].replace("[", "").replace("]", "").split(",")
    z.pop()
    z.pop()
    z.pop()
    z.pop()
    z = map(eval, z)
    if z == zero:
        # print (lines.index(line))
        lines.remove(line)
# for line in lines:
#     print line
for line in lines: #68维
    x = line.split(" ",1)[1].replace("[","").replace("]","").split(",")
    x = map(eval, x)
    #x=np.array(x,dtype=float)
    y = line.split(" ", 1)[0]
    y = int(y)

    if y==0:
        y=[1,0,0,0,0]
    elif y==1:
        y = [0, 1, 0, 0, 0]
    elif y==2:
        y = [0, 0, 1, 0, 0]
    elif y==3:
        y = [0, 0, 0, 1, 0]
    elif y==4:
        y = [0, 0, 0, 0, 1]
    X.append(x)
    Y.append(y)

x_train = np.array(X)
y_train = np.array(Y)

print x_train
print y_train
f = codecs.open("csj_vec.txt",'r','utf-8')    #codecs包指定TXT打开方式

lines=f.readlines()

X=[]
Y=[]

zero=[]
for i in range(0,64):
    zero.append(0)
print zero
for line in lines:
    z=line.split(" ",1)[1].replace("[","").replace("]","").split(",")
    z.pop()
    z.pop()
    z.pop()
    z.pop()
    z = map(eval, z)
    if z==zero:
        #print (lines.index(line))
        lines.remove(line)
for line in lines: #68维
    x = line.split(" ",1)[1].replace("[","").replace("]","").split(",")
    x = map(eval, x)
    #x=np.array(x,dtype=float)
    y = line.split(" ", 1)[0]
    y = int(y)

    if y==0:
        y=[1,0,0,0,0]
    elif y==1:
        y = [0, 1, 0, 0, 0]
    elif y==2:
        y = [0, 0, 1, 0, 0]
    elif y==3:
        y = [0, 0, 0, 1, 0]
    elif y==4:
        y = [0, 0, 0, 0, 1]
    X.append(x)
    Y.append(y)

x_test = np.array(X)
y_test = np.array(Y)

print x_test
print y_test
# x_train = np.random.random((10, 10))  #20维度,1000个   #1000个1维 10类,
# y_train = keras.utils.to_categorical(np.random.randint(3, size=(10, 1)), num_classes=3)  #10类,1000个10维
# x_test = np.random.random((10, 10))
# y_test = keras.utils.to_categorical(np.random.randint(3, size=(10, 1)), num_classes=3)

# x_train= np.array([[1.9629345734914143,3.196568012237549,0.23380190134048462],[-1.5588303009668987,0.39092262585957843,0.4524924159049988],[0.326497220993042,-5.757664465904236,1.7457123756408692],[0.326497220993042,-5.757664465904236,1.7457123756408692]])

# y_train=np.array([[1,0,0],[0,1,0],[0,0,1],[0,0,1]])

# x_test= np.array([[1.9629345734914143,3.196568012237549,0.23380190134048462],[-1.5588303009668987,0.39092262585957843,0.4524924159049988],[0.326497220993042,-5.757664465904236,1.7457123756408692],[0.326497220993042,-5.757664465904236,1.7457123756408692]])

# y_test=np.array([[1,0,0],[0,1,0],[0,0,1],[0,0,1]])


#创建一个模型
model = Sequential()

print (x_train[0])
print (len(x_train))
print (y_train[0])
print (len(y_train))

print (y_train[1])
print (x_test[0])
print (y_test[0])

# 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(64, activation='relu', input_dim=68))#输入维度
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(5, activation='softmax')) #分类类别数

#使用岁时函数和优化函数
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

#开始训练
a=model.fit(x_train, y_train,
          epochs=20, #训练轮数
          batch_size=128)

#损失函数和精确度
score = model.evaluate(x_test, y_test, batch_size=128)
print('Test loss:', score[0])
print('Test accuracy:', score[1])


#预测
classes = model.predict(x_test, batch_size=128)
print classes

猜你喜欢

转载自blog.csdn.net/qq_35398413/article/details/81559945
今日推荐