새로운 TensorFlow 모형을 만들어 보겠습니다. 이렇게 시작합니다.
Let’s create a new TensorFlow model. It starts like this.
class MyModel(tf.keras.Model):
def __init__(self):
super().__init__() def call(self):
return
설명해야 할 것이 엄청 많지만, 잘 모르겠는 것은 일단 받아들이면 됩니다. 이것이 최소한의 구조입니다. 지금부터 하나 하나씩 붙여나가면 됩니다.
There is a lot to explain, but if you are not sure, you can accept it. This is the minimal structure. From now on, you can add one by one.
모형을 만들었는데, 어떻게 실행할까요?
How to run it?
a = MyModel()
class의 이름을 불러주고, 새로운 변수에 저장시킵니다. 이 모형은 별 일을 못합니다. 아무것도 return하지 않기 때문입니다. 최소한의 무엇이라도 하게 하기 위하여 바꾸어 보겠습니다.
Call the name of the class and save it in a new variable. This model doesn’t do much. Because it doesn’t return anything. Let’s change it to do at least something.
class MyModel(tf.keras.Model):
def __init__(self):
super().__init__() def call(self, x):
return x
다시 부르고, 실행시켜 보겠습니다.
Call it again and let’s run it.
a = MyModel()
a(1)<tf.Tensor: shape=(), dtype=int32, numpy=1>
엄청난 일이 일어났습니다. 1이라는 값을 주었더니 <tf.Tensor: shape=(), dtype=int32, numpy=1>
를 반환하는 TensorFlow 모형을 만들었습니다. 이제 어떠한 모형도 다 만들 수 있습니다.
An amazing thing has happened. I created a TensorFlow model that gives me a value of 1 and returns <tf.Tensor: shape=(), dtype=int32, numpy=1> Now you can make any model.
SimpleRNN layer를 추가해 보겠습니다. 두가지를 기억하면 됩니다. init
에서 정의하고, call
에서 사용한다입니다.
Let’s add a SimpleRNN layer. There are two things to remember. Define it in init and use it in call.
class MyModel(tf.keras.Model):
def __init__(self):
super().__init__()
self.rnn = tf.keras.layers.SimpleRNN(3) def call(self, x):
y = self.rnn(x)
return y
이번에는 그냥 1을 넣으면 error가 발생합니다. 왜냐하면, RNN layer의 입력은 이런 모양이어야 하기 때문입니다.
This time I just put 1 and I get an error. This is because the input of the RNN layer should look like this.
d = np.array([1,2,3,4,5,6,7]).astype(np.float32)
x = d.reshape(1,-1,1)a = MyModel()
a(x)
오류없이 잘 돌아갔습니다. <tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[-0.999857 , 0.9999839, -0.9462582]], dtype=float32)>
여기 나오는 소수점 숫자는 같지 않아도 됩니다. 다만, Tensor shape이 다르면 안됩니다.
It worked fine without any errors. <tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[-0.999857 , 0.9999839, -0.9462582]], dtype=float32)> The decimal point numbers shown here do not have to be the same. However, the Tensor shape must not be different.