03_01_logistic_regressioni_or

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

np.random.seed(0)

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
Y = np.array([[0], [1], [1], [1]])


model = Sequential([
# Dense(input_dim=2, output_dim=1), # Keras 1
Dense(input_dim=2, units=1), # Keras 2
Activation('sigmoid')
])

model.compile(loss='binary_crossentropy', optimizer=SGD(lr=0.1))

# model.fit(X, Y, nb_epoch=200, batch_size=1) # Keras 1
model.fit(X, Y, epochs=200, batch_size=1) # Keras 2

classes = model.predict_classes(X, batch_size=1)
prob = model.predict_proba(X, batch_size=1)

print('classified:')
print(Y == classes)
print()
print('output probability:')
print(prob)

classified:
[[ True]
 [ True]
 [ True]
 [ True]]


output probability:
[[0.20687966]
 [0.92517704]
 [0.91522515]
 [0.99804986]]




阅读更多

没有更多推荐了,返回首页