keras.SimpleRNNの設定3

前提

先の設定2から、時系列データを再現性、正当性を確認しやすくするため、変更しました。
そして、そうしなければならない理由は不明ですが、訓練データ(l_x)と対応する訓練ラベル(l_y)のshapeを合わせるために、
訓練ラベルを本来不要な冗長なデータを付加した。
その結果、設定2にあった次元の不一致と思われるエラーは通過しました。
しかし、下記に示すwarninngが出ます。
今回は、このwarninngの意味についてお尋ねします。最後にも別なエラーもありますがとりあえず直接的には不問にします。
よくわからないのは、モデルがshape (None, 6, 1)として構築されている言う部分で、
モデル設定のどの部分についての事かが正確には不明です。

発生している問題・エラーメッセージ(改行挿入)

WARNING:tensorflow:Model was constructed with shape (None, 6, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 6, 1), dtype=tf.float32, name='simple_rnn_11_input'), name='simple_rnn_11_input', description="created by layer 'simple_rnn_11_input'"), but it was called on an input with incompatible shape (10, 6, 3). Traceback (most recent call last):

該当のソースコード

python

import numpy as np import tensorflow as tf from tensorflow import keras import random from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Activation from tensorflow.keras.layers import SimpleRNN ts = 6 #ベクトル長feature = 1ncat=3bat = 10 #ベクトル個数unit = 3dense_unit = ncat #binary_crossentropy model = Sequential()model.add(SimpleRNN(unit, input_shape=(ts,feature), return_sequences=True)) model.add(Dense(dense_unit, activation="linear"))model.compile(loss='categorical_crossentropy')#model.compile(loss='binary_crossentropy') model.summary() y = np.zeros(60)# 次の長さ6の変動を反復する時系列データを生成。y[0] = 0y[1] = 1y[2] = 1y[3] = 0y[4] = 0y[5] = -1 for n in range(1,10): for i in range(0,6): y[6*n+i] = y[i]print(y) #y = l_x.astype("float32")print("y.size",y.size) l_x = np.zeros((bat,ts,ncat))l_y = np.zeros((bat, ts)) cl_y = np.zeros((bat,ts,ncat))print("cl_y.shape",cl_y.shape)#print(cl_y) for offs in range(1): # offs=0 の場合のみ for i in range(0, bat): for j in range(0, ts): l_x[i,j] = y[i+offs +j] l_y[i,j] = y[i+offs+1 ] for nc in range(0,ncat): cl_y[i,j,nc]=0 nc = l_y[i,j].astype('int') #print("nc",nc) cl_y[i,j,nc] = 1      #前記はonehotolabelを生成する関数を使わずに、生成した。(次元の関係を明確にするため) #cl_y[i,0] = tf.keras.utils.to_categorical(l_y[i,0], 2) #print(i,j,"cl_y",cl_y[i,j]) #tr_x = l_x.reshape(bat, ts, 1) #tr_y = l_y.reshape(bat, 1) print("tr_x.size",l_x.size) print("tr_y.size",l_y.size) print("shape",l_x.shape,l_y.shape,cl_y.shape) history = model.fit(l_x, cl_y, epochs=1 ,batch_size=bat, verbose=1)

生成した訓練データと教師データの関係

時刻    訓練データ     教師データ T=0 vec0 t0,t1,t2,t3,t4   teach0 = x,x,x,x,x,t5 T=1 vec1 t1,t2,t3,t4,t5 teach1 = x,x,x,x,x,t6 T=2 vec2 t2,t3,t4,t5,t6 teach2 = x,x,x,x,x,t7 T=3 vec3 t3,t4,t5,t6,t7 teach3 = x,x,x,x,x,t8 T=4 vec4 t4,t5,t6,t7,t8 teach4 = x,x,x,x,x,t9 T=5 vec5 t5,t6,t7,t8,t9 teach5 = x,x,x,x,x,t10 T=6 vec6 t6,t7,t8,t9,t10 teach6 = x,x,x,x,x,t11 T=7 vec7 t7,t8,t9,t10,t11 teach7 = x,x,x,x,x,t12 T=8 vec8 t8,t9,t10,t11,t12 teach8 = x,x,x,x,x,t13 T=9 vec9 t9,t10,t11,t12,t13 teach9 = x,x,x,x,x,t14 x:訓練データと次元を合わせるために、教師データに挿入したダミーデータ ただし、ベクトル長は設定にのままで、正しくは6stepとなります。

コンソールリスト1

注目しているwarninngがある

runfile('C:/book/rnn1/test5.py', wdir='C:/book/rnn1')
Model: "sequential_12"
__
Layer (type) Output Shape Param #
simple_rnn_11 (SimpleRNN) (None, 6, 3) 15

dense_11 (Dense) (None, 6, 3) 12

==
Total params: 27
Trainable params: 27
Non-trainable params: 0


[ 0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1.
0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1.
0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1. 0. 1. 1. 0. 0. -1.
0. 1. 1. 0. 0. -1.]
y.size 60
cl_y.shape (10, 6, 3)
tr_x.size 180
tr_y.size 60
shape (10, 6, 3) (10, 6) (10, 6, 3)
WARNING:tensorflow:Model was constructed with shape (None, 6, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 6, 1), dtype=tf.float32, name='simple_rnn_11_input'), name='simple_rnn_11_input', description="created by layer 'simple_rnn_11_input'"), but it was called on an input with incompatible shape (10, 6, 3).
Traceback (most recent call last):

このwarninngに注目

コンソールリスト2

File "C:\Users\qhtsi.conda\envs\py38tfkr\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)

File "c:\book\rnn1\test5.py", line 71, in <module>
history = model.fit(l_x, cl_y, epochs=1 ,batch_size=bat, verbose=1)

File "C:\Users\qhtsi.conda\envs\py38tfkr\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None

File "C:\Users\qhtsi\AppData\Local\Temp_autograph_generated_file4ehvanpt.py", line 15, in tf__train_function
retval
= ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)

ValueError: in user code:
File "C:\Users\qhtsi.conda\envs\py38tfkr\lib\site-packages\keras\engine\training.py", line 1051, in train_function *
return step_function(self, iterator)
File "C:\Users\qhtsi.conda\envs\py38tfkr\lib\site-packages\keras\engine\training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\qhtsi.conda\envs\py38tfkr\lib\site-packages\keras\engine\training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "C:\Users\qhtsi.conda\envs\py38tfkr\lib\site-packages\keras\engine\training.py", line 889, in train_step
y_pred = self(x, training=True)
File "C:\Users\qhtsi.conda\envs\py38tfkr\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\qhtsi.conda\envs\py38tfkr\lib\site-packages\keras\engine\input_spec.py", line 264, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Exception encountered when calling layer "sequential_12" (type Sequential).
Input 0 of layer "simple_rnn_11" is incompatible with the layer: expected shape=(None, None, 1), found shape=(10, 6, 3)
Call arguments received by layer "sequential_12" (type Sequential):
• inputs=tf.Tensor(shape=(10, 6, 3), dtype=float32)
• training=True
• mask=None

コメントを投稿

0 コメント