-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
executable file
·61 lines (48 loc) · 1.7 KB
/
Copy pathtrain.py
File metadata and controls
executable file
·61 lines (48 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
# Load the training data, format: x, y, label
dataset = np.loadtxt('moon_data.csv', delimiter=',')
data = dataset[:,0:2]
labels = dataset[:,2]
plt.scatter(data[:,0], data[:,1], s=40, c=labels, cmap=plt.cm.Spectral)
plt.show()
# Split the data into training and test data
data_train, data_test, labels_train, labels_test = train_test_split(data, labels)
# Build the model
model = Sequential()
model.add(Dense(2, activation='linear'))
model.add(Dropout(0.001))
model.add(Dense(5, activation='relu'))
model.add(Dropout(0.001))
model.add(Dense(1, activation='sigmoid'))
# Configure a model for mean-squared error regression.
model.compile(loss='mse', optimizer='adam')
checkpointer = keras.callbacks.ModelCheckpoint(filepath="weights.h5", verbose=1, save_best_only=True)
# Train the model
history = model.fit(data_train, labels_train, epochs=5000, batch_size=32, verbose=1, validation_split=0.2, callbacks=[checkpointer])
model.summary()
print(history.history.keys())
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.show()
# Evaluate
dataset = np.loadtxt('moon_data_test.csv', delimiter=',')
data = dataset[:,0:2]
labels = dataset[:,2]
print(model.evaluate(data, labels))
print(model.metrics_names)
# Predict
print(model.predict(data), labels)
#print(labels)
#for i in range(len(labels)):
# print(model.predict(data)[i][0], labels[i])
#model.predict(np.array([[1.2,2.3]]))