0
回答
python keras type错误?
【腾讯云】校园拼团福利,1核2G服务器10元/月!>>>   

代码如下:

1# coding=utf-8
2 import keras
3 import theano
4 from theano import configparser
5 import numpy as np
6 np.random.seed(123)
7 import mkl
8 from keras.models import Sequential
9 from keras.layers import Dense, Activation
10 from keras.optimizers import SGD
11
12 dataMat1 = []
13 labelMat1 = []
14 dataMat2 = []
15 labelMat2 = []
16 fr1 = open(r'F:\train1.txt')
17 fr2 = open(r'F:\test1.txt')
18 for line in fr1.readlines():
19 lineArr = line.strip().split()
20 dataMat1.append([(lineArr[0]),(lineArr[1]),(lineArr[2])])
21 labelMat1.append((lineArr[3]))
22 for line in fr2.readlines():
23 lineArr = line.strip().split()
24 dataMat2.append([(lineArr[0]),(lineArr[1]),(lineArr[2])])
25 labelMat2.append((lineArr[3]))
26
27 dataMat1=list(dataMat1)
28 labelMat1=list(labelMat1)
29 dataMat2=list(dataMat2)
30 labelMat2=list(labelMat2)
31
32 dataMat1=map(str,dataMat1)
33 labelMat1=map(str,labelMat1)
34 dataMat2=map(str,dataMat2)
35 labelMat2=map(str,labelMat2)
36
37 dataMat1=np.array(dataMat1)
38 labelMat1=np.array(labelMat1)
39 dataMat2=np.array(dataMat2)
40 labelMat2=np.array(labelMat2)
41
42 X_train, Y_train = dataMat1, labelMat1
43 X_test, Y_test =dataMat2, labelMat2
44
45
46 model = Sequential()
47 model.add(Dense(3, input_dim=3))
48 model.add(Dense(1, output_dim=1,activation='relu'))
49
50 sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
51 model.compile(loss='mse', optimizer='sgd')
52
53 print ("Training",'\r\n')
54 cost = model.fit(X_train, Y_train, nb_epoch=70, batch_size=10)
55 print('cost:',cost)
56
57 print ("Testing",'\r\n')
58 cost = model.evaluate(X_test, Y_test , batch_size=3)
59 print ('test loss: ', cost,'\r\n')
60
61 W, b = model.layers[1].get_weights()
62 print ('Weights:', W,'\r\n')
63 print ('Biases:' , b,'\r\n')

举报
发撒而过
发帖于3个月前 0回/52阅
顶部