import numpy as np
import tensorflow as tf
x = np.array([[0,0], [1,0], [1,1], [0,0], [0,0], [0,1]]) # 바퀴, 날개
y = np.array([
[1,0,0], # 배
[0,1,0], # 자동차
[0,0,1], # 비행기
[1,0,0],
[1,0,0],
[0,0,1]
])
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(input_dim=2, units=10, activation='relu')) # input_dim : 입력값 갯수
model.add(tf.keras.layers.Dense(units=5, activation='relu')) # units : 출력값 갯수
model.add(tf.keras.layers.Dense(units=3, activation='softmax'))
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.01), loss='categorical_crossentropy', metrics=['accuracy'])
# model.summary()
history = model.fit(x, y, epochs=100, batch_size=1)
for weight in model.weights:
print(weight)
loss = model.evaluate(x,y,batch_size=1)
print(loss)
print("====================================")
print(x)
print(model.predict(x))
print("Accuracy: %.4f" % model.evaluate(x,y)[1])
print("====================================")
epoch 횟수에 따른 loss와 accuracy를 그래프로 표현하면 다음과 같다.
import matplotlib.pyplot as plt
plt.figure(figsize=(12,4))
plt.subplot(1,1,1)
plt.plot(history.history['loss'], 'b--', label='loss')
plt.plot(history.history['accuracy'],'g-',label='Accuracy')
plt.xlabel('Epoch')
plt.legend()
plt.show()