잠토의 잠망경

[ML] CNN - Multiple Parallel Input and Multi-step Output 본문

공부/Python

[ML] CNN - Multiple Parallel Input and Multi-step Output

잠수함토끼 2020. 2. 16. 16:47

GITHUB

https://github.com/yiwonjae/Project_Lotto/blob/master/Book_001/p119.py

 

0. 목표

[ 10 15 25]
[ 20 25 45]
[ 30 35 65]
[ 40 45 85]
[ 50 55 105]
[ 60 65 125]
[ 70 75 145]
[ 80 85 165]
[ 90 95 185]


input
[ 10 15 25]
[ 20 25 45]
[ 30 35 65]

output
[ 40 45 85]
[ 50 55 105]

 

1. DATA

 

in_seq1 = np.asarray([10, 20, 30, 40, 50, 60, 70, 80, 90])
in_seq2 = np.asarray([15, 25, 35, 45, 55, 65, 75, 85, 95])
out_seq = np.asarray([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))])

in_seq1:ndarray = in_seq1.reshape((len(in_seq1), 1))
in_seq2:ndarray = in_seq2.reshape((len(in_seq2), 1))
out_seq:ndarray = out_seq.reshape((len(out_seq), 1))

dataset = np.hstack((in_seq1, in_seq2, out_seq))

 

2. DATA 정제

X

import numpy as np
from numpy import ndarray


def split_sequence(sequence:ndarray, n_steps_in:int, n_steps_out:int)->(ndarray, ndarray):

    x=[]
    y=[]

    for i in range(len(sequence)):
        if(i+n_steps_in+n_steps_out>len(sequence)):
            break

        x.append(sequence[i:i+n_steps_in, :])
        y.append(sequence[i+n_steps_in:i+n_steps_in+n_steps_out, :])


    return (np.asarray(x), np.asarray(y))
    
    
n_steps_in  = 3
n_steps_out = 2

(x, y) = split_sequence(dataset, n_steps_in, n_steps_out)

print(x.shape)
print(y.shape)

n_output = y.shape[1]*y.shape[2]
y:ndarray = y.reshape((y.shape[0], n_output))

n_features = x.shape[2]

print(y.shape)

 

3. 학습

from keras import Sequential
from keras.layers import Dense, Flatten
from keras.layers.convolutional import Conv1D, MaxPooling1D


model = Sequential()
model.add(Conv1D(64, 2, activation='relu', input_shape=(n_steps_in, n_features)))
model.add(MaxPooling1D())
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(n_output))
model.compile(loss='mse', optimizer='adam')

model.fit(x, y, epochs=2000, verbose=1)

 

 

4. 표시

x_input = np.asarray([[60, 65, 125], [70, 75, 145], [80,85,165]])   # expect [90, 95, 185], [100, 105, 205]
x_input = x_input.reshape(1, n_steps_in, n_features)

yhat = model.predict(x_input, verbose=1)

print(yhat) # [[ 90.66462  96.08125 186.5697  101.55215 106.85993 208.24315]]
Comments