import numpy as np
import tensorflow as tf
def solution_model():
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([5.0, 6.0, 7.0, 8.0, 9.0, 10.0], dtype=float)
# YOUR CODE HERE
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(1, input_shape=[1]),
])
model.compile(optimizer="sgd", loss="mse")
model.fit(xs, ys, epochs=500, verbose=0)
return model
# Note that you'll need to save your model as a .h5 like this
# This .h5 will be uploaded to the testing infrastructure
# and a score will be returned to you
if __name__ == '__main__':
model = solution_model()
model.save("mymodel.h5")
print(model.predict([10.0]))
def sigmoid(z):
return 1/(1+np.exp(-z))
plt.figure(figsize=(10, 7))
x = np.arange(-10, 10)
y = sigmoid(x)
plt.plot(x, y)
plt.show()
방법2)
Dense(2, activation = 'softmax')
loss = 'categorical_crossentropy' (y값이 onehot 인코딩 되어있을 때)
또는
loss = 'sparse_categorical_crossentropy' (y값이 onehot 인코딩 안되어 있을 때)
각각의 뉴런이 될 확률을 구해서 가장 큰 값을 가진 뉴런으로 분류
A가 될 확률 : 0.7
B가 될 확률 : 0.2
C가 될 확률 : 0.1
--> A로 분류
import numpy as np
a = np.random.uniform(low=0.0, high=10.0, size=3)
def softmax(a) :
exp_a = np.exp(a)
sum_exp_a = np.sum(exp_a)
y = exp_a / sum_exp_a
return y
y = softmax(a)
print('Class 별 확률 값 출력')
print('===' * 10)
for i in range(3):
print('Class {} 의 확률: {:.3f}'.format(i, y[i]))
print('===' * 10)
print('Class 별 확률 값의 합: {:.1f}'.format(y.sum()))
Flatten이란?
고차원을 1D로 변환하여Dense Layer에 전달해 주기 위하여 사용합니다.
28 X 28 의2D로 되어 있는 이미지를 784로1D로 펼쳐 주는 작업입니다.
이미지는 2차원 데이터(2D)
2D데이터는 Dense Layer에 못들어간다.
2차원 데이터를 1차원으로 변환해주어야 함
5. Model 만들기
model 정의 할 때 input_shape 잡는 법
input 으로 들어가는 x_train의 shape를 확인하면 된다. 해당 결과를 그대로 사용하면 된다 (28,28)