ANN 구조를 이용해 한국의 여러 경제 지표를 통해 경기 정•저점을 판정

한국의 경기정저점.pdf

termpaper2.ipynb

사용한 data file (원하는 데이터만 추출했기 때문에 난잡해 보일 수 있음)

data1.xlsx

import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt
%matplotlib inline
x_train = df.iloc[173:235, [1,2,5,6,7,8,9]].values  # from 1991.1m~2007.12m
y_train = df.iloc[173:235, [10]].values
#x_train.tail()
x_train = np.array(x_train, dtype=np.float32)
y_train = np.array(y_train, dtype=np.float32)
print(x_train.shape, y_train.shape)

x_test = df.iloc[160:170, [1,2,5,6,7,8,9]].values  # from 2008.1m~2009.2m
y_test = df.iloc[160:170, [10]].values
#print(x_test.tail())
x_test = np.array(x_test, dtype=np.float32)
y_test = np.array(y_test, dtype=np.float32)

scaler = StandardScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.fit_transform(x_test)

(62, 7) (62, 1)

# dataset parameters
nb_classes  = y_train.shape[1] # 1 종속변수의 수:1개
nb_features = x_train.shape[1] # the number of variables 6 독립변수의 수: 6개
print(nb_classes, nb_features)

n_train = x_train.shape[0] # the number of sample 204 train수
n_test  = x_test.shape[0]  # the number of sample 13 test수
print(n_train, n_test)

# training HyperParameters
learning_rate  = 0.01  # 0.005
training_steps = 18001 # 1000001 학습횟수
display_step   = 2000

# network hyperparameters
n_hidden1 = 8 # 1st layer number of neurons
n_output1 = 1

1 7 62 10

# define weights as a dictionary 초기값 set up
tf.random.set_seed(2021)
"""
weights = {
    'h1'  : tf.Variable(tf.random.normal([nb_features, n_hidden1], stddev=0.1)),
    'out' : tf.Variable(tf.random.normal([n_hidden1, n_output1], stddev=0.1))
}
biases = { 상수
    'b1'  : tf.Variable(tf.random.normal([n_hidden1], stddev=0.1)),
    'out' : tf.Variable(tf.random.normal([n_hidden1], stddev=0.1))
}
"""
weights = {
    #'h1' : tf.Variable(tf.initializers.GlorotUniform()((nb_features, n_hidden1))),
    #'out' : tf.Variable(tf.initializers.GlorotUniform()((nb_features, n_output))) #xavier_initializer_())
    'h1'  : tf.Variable(tf.initializers.GlorotNormal()((nb_features, n_hidden1))),
    'out' : tf.Variable(tf.initializers.GlorotNormal()((n_hidden1, n_output1))) #xavier_initializer_())
}
biases = {
    'b1'  : tf.Variable(tf.random.normal([n_hidden1], stddev=0.1)),
    'out' : tf.Variable(tf.random.normal([n_output1], stddev=0.1))
}
# define network architecture
def build_model(X):    
    hidden = tf.add(tf.matmul(X, weights['h1']), biases['b1'])
    hidden = tf.nn.relu(hidden) # tf.nn.sigmoid(hidden) sigmoid대신 ReLU사용
    
    output = tf.add(tf.matmul(hidden, weights['out']), biases['out'])
    return tf.nn.sigmoid(output) #0 또는 1의 값이 나가야하기 때문에

# cost function : cross-entropy, binary 때 사용한거
def cost(H, Y):
    return -tf.reduce_mean(Y*tf.math.log(H) + (1-Y)*tf.math.log(1-H))

# Accuracy, binary 때 사용한거
def accuracy(X, Y):
    hypo = build_model(X)
    predicted = tf.cast(hypo > 0.5, dtype=tf.float32)
    return tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
# stochastic gradient descent algorithm to minimize cost funtion
optimizer = tf.optimizers.SGD(learning_rate)

# training process
loss_record = []
def train(X, Y):
    # wrap computation inside a GradientTape for automactic differentiation
    with tf.GradientTape() as g:
        pred = build_model(X)  # make prediction
        loss = cost(pred, Y)  # calculate loss
        loss_record.append(loss.numpy())
        #print('loss = ',loss.numpy())

    # calculate gradients
    #gradients = g.gradient(loss, [W, b]) # dloss/dW, dloss/db
    #optimizer.apply_gradients(zip(gradients, [W, b])) # W : W - a*dcost/dW
        
    # Variables to update, i.e. trainable variables. 변수를 정확히 지정하는 부분이 다름
    trainable_variables = list(weights.values()) + list(biases.values())

    # Compute gradients.
    gradients = g.gradient(loss, trainable_variables)
    
    # Update W and b following gradients.
    optimizer.apply_gradients(zip(gradients, trainable_variables))

for step in range(training_steps):
    # run the optimiazation to update W and b
    train(x_train, y_train)
    
    if step % display_step == 0:
        pred = build_model(x_train)  # make prediction
        loss = cost(pred, y_train)   # calculate loss
        acc  = accuracy(x_train, y_train)
        print("%s \t cost = %s \t accuracy = %f" % (step, loss.numpy(), acc.numpy()))

0 cost = 0.6650865 accuracy = 0.500000 2000 cost = 0.37959784 accuracy = 0.838710 4000 cost = 0.3019726 accuracy = 0.838710 6000 cost = 0.25943106 accuracy = 0.903226 8000 cost = 0.23054793 accuracy = 0.903226 10000 cost = 0.2090987 accuracy = 0.935484 12000 cost = 0.19144978 accuracy = 0.951613 14000 cost = 0.17739293 accuracy = 0.951613 16000 cost = 0.16556905 accuracy = 0.951613 18000 cost = 0.15506113 accuracy = 0.951613

plt.figure(figsize = (8,5))
plt.plot(loss_record)
plt.xlabel('iteration', fontsize = 15)
plt.ylabel('loss', fontsize = 15)
plt.show()

acc = accuracy(x_train, y_train).numpy()
print("Accuracy: %s" % (acc*100))

Accuracy: 95.16128897666931