<jupyter_start><jupyter_text>RNN의 각 Step에 넣어줄 데이터 준비 - 사전 연습<jupyter_code>import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import math
import matplotlib.pyplot as plt
%matplotlib inline
print(tf.__version__)
sess = tf.InteractiveSession()
batch_size = 3
n_steps = 4
n_inputs = 5

values = []
for i in range(0, batch_size):
    values.append([])
    for j in range(0, n_steps):
        values[i].append([])
        for k in range(0, n_inputs):
            values[i][j].append(k + j + (i * 5))
            
values_array = np.asarray(values)
x = tf.constant(value = values_array)
print(x)
print(x.eval())
xt = tf.transpose(x, perm = [1, 0, 2])
print(xt)
print(xt.eval())
xr = tf.reshape(xt, [-1, n_inputs])  # n_inputs = 5
print(xr)
print(xr.eval())
xs = tf.split(value = xr, num_or_size_splits = n_steps, axis=0)  # n_steps = 4
print(type(xs))
print()
print(xs)
print()
for item in xs:
    print(item.eval())
    print()<jupyter_output><class 'list'>

[<tf.Tensor 'split:0' shape=(3, 5) dtype=int64>, <tf.Tensor 'split:1' shape=(3, 5) dtype=int64>, <tf.Tensor 'split:2' shape=(3, 5) dtype=int64>, <tf.Tensor 'split:3' shape=(3, 5) dtype=int64>]

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]

[[ 2  3  4  5  6]
 [ 7  8  9 10 11]
 [12 13 14 15 16]]

[[ 3  4  5  6  7]
 [ 8  9 10 11 12]
 [13 14 15 16 17]]<jupyter_text>MNIST 데이터에 대한 RNN 배치 Input 처리 - 사전연습<jupyter_code>mnist = input_data.read_data_sets("../MNIST_data/", one_hot=True)
# image: 28 * 30
batch_size = 128
n_steps = 28
n_inputs = 28

x = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
print(x.get_shape())
print()

xt = tf.transpose(x, perm = [1, 0, 2])
print(xt.get_shape())
print()

xr = tf.reshape(xt, shape = [-1, n_inputs])
print(xr.get_shape())
print()

xs = tf.split(value=xr, num_or_size_splits=n_steps, axis=0)
print(len(xs))
for item in xs:
    print(item)<jupyter_output>(?, 28, 28)

(28, ?, 28)

(?, 28)

28
Tensor("split_1:0", shape=(?, 28), dtype=float32)
Tensor("split_1:1", shape=(?, 28), dtype=float32)
Tensor("split_1:2", shape=(?, 28), dtype=float32)
Tensor("split_1:3", shape=(?, 28), dtype=float32)
Tensor("split_1:4", shape=(?, 28), dtype=float32)
Tensor("split_1:5", shape=(?, 28), dtype=float32)
Tensor("split_1:6", shape=(?, 28), dtype=float32)
Tensor("split_1:7", shape=(?, 28), dtype=float32)
Tensor("split_1:8", shape=(?, 28), dtype=float32)
Tensor("split_1:9", shape=(?, 28), dtype=float32)
Tensor("split_1:10", shape=(?, 28), dtype=float32)
Tensor("split_1:11", shape=(?, 28), dtype=float32)
Tensor("split_1:12", shape=(?, 28), dtype=float32)
Tensor("split_1:13", shape=(?, 28), dtype=float32)
Tensor("split_1:14", shape=(?, 28), dtype=float32)
Tensor("split_1:15", shape=(?, 28), dtype=float32)
Tensor("split_1:16", shape=(?, 28), dtype=float32)
Tensor("split_1:17", shape=(?, 28), dtype=float32)
Tensor("split_1:18", shape=(?, 28), dtype=float32)
Ten[...]<jupyter_text>Sine Function 데이터에 대한 RNN 배치 Input 처리 - 사전연습<jupyter_code>import numpy as np

def split_data(data, val_size=0.1, test_size=0.2):
    """
    splits data to training, validation and testing parts
    """
    ntest = int(round(len(data) * (1 - test_size)))
    nval = int(round(len(data) * (1 - (val_size + test_size))))

    train_data, val_data, test_data = data[:nval], data[nval:ntest], data[ntest:]
    return train_data, val_data, test_data

def prepare_data(data, n_steps):
    x = []
    y = []
    for i in range(len(data) - n_steps - 1):
        x_item = []
        for j in range(n_steps):
            x_item.append([data[i + j]])
        x.append(x_item)
        y.append([data[i + n_steps]])
    x = np.asarray(x)
    y = np.asarray(y)
    return x, y    

def generate_data(func, x, n_steps):
    """
    generates data with based on a function fct
    creates new data frame based on previous observation
       * example:
        l = [1, 2, 3, 4, 5]
        n_steps = 3
        -> [[[1], [2], [3]], [[2], [3], [4]], [[3], [4], [5]]]
    """
    data = func(x)
    train_data, val_data, test_data = split_data(data, val_size=0.1, test_size=0.2)
    train = {}
    val = {}
    test = {}
    train['x'], train['y'] = prepare_data(train_data, n_steps)
    val['x'], val['y'] = prepare_data(val_data, n_steps)
    test['x'], test['y'] = prepare_data(test_data, n_steps)
    return train, val, test
n_steps = 5
train, val, test = generate_data(np.sin, np.linspace(0, 100, 10000), n_steps)
print(train['x'][0:5], len(train['x']))
print(train['y'][0:5], len(train['y']))
print()
print(val['x'][0:5], len(val['x']))
print(val['y'][0:5], len(val['y']))
print()
print(test['x'][0:5], len(test['x']))
print(test['y'][0:5], len(test['y']))<jupyter_output>[[[ 0.        ]
  [ 0.01000083]
  [ 0.02000067]
  [ 0.0299985 ]
  [ 0.03999333]]

 [[ 0.01000083]
  [ 0.02000067]
  [ 0.0299985 ]
  [ 0.03999333]
  [ 0.04998416]]

 [[ 0.02000067]
  [ 0.0299985 ]
  [ 0.03999333]
  [ 0.04998416]
  [ 0.05997   ]]

 [[ 0.0299985 ]
  [ 0.03999333]
  [ 0.04998416]
  [ 0.05997   ]
  [ 0.06994983]]

 [[ 0.03999333]
  [ 0.04998416]
  [ 0.05997   ]
  [ 0.06994983]
  [ 0.07992267]]] 6994
[[ 0.04998416]
 [ 0.05997   ]
 [ 0.06994983]
 [ 0.07992267]
 [ 0.08988751]] 6994

[[[ 0.77830536]
  [ 0.78454582]
  [ 0.79070781]
  [ 0.79679071]
  [ 0.80279392]]

 [[ 0.78454582]
  [ 0.79070781]
  [ 0.79679071]
  [ 0.80279392]
  [ 0.80871684]]

 [[ 0.79070781]
  [ 0.79679071]
  [ 0.80279392]
  [ 0.80871684]
  [ 0.81455887]]

 [[ 0.79679071]
  [ 0.80279392]
  [ 0.80871684]
  [ 0.81455887]
  [ 0.82031942]]

 [[ 0.80279392]
  [ 0.80871684]
  [ 0.81455887]
  [ 0.82031942]
  [ 0.82599793]]] 994
[[ 0.80871684]
 [ 0.81455887]
 [ 0.82031942]
 [ 0.82599793]
 [ 0.83159383]] 994

[[[-0.99[...]
{'fraction_non_alphanumeric': 0.2008004176091874, 'fraction_numerical': 0.14859926918392205, 'mean_word_length': 3.6542510121457488, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '151397', 'license': 'MIT', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2548, 'n_words': 537, 'path': '1.DeepLearning/08.RNN/prerequisite.ipynb', 'repo_name': 'link-kut/deeplink_public', 'size': '5747'}