3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

import routeにご注意

Posted at

結論

違うルートからimportしたクラス、違うものと認識される。

例:エラー発生時

import

import tensorflow as tf
from tensorflow import keras
from keras.layers import Dense  # [1]

sess = tf.Session()

[1] Denseを短い方式で利用できるようにimportしました

model作る

model = keras.Sequential()
model.add(keras.layers.Dense(8, input_shape=(10, 16))) # [2]
model.add(Dense(32)) # [3] 
model.summary()

[2] 長い方式でDense利用
[3] 短い方式でDense利用

実行したらエラー

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-81-6aa9bc4246b0> in <module>()
      1 model = keras.Sequential()
      2 model.add(keras.layers.Dense(8, input_shape=(10, 16)))
----> 3 model.add(Dense(32))
      4 model.summary()

~/tensorflow/venv/local/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py in _method_wrapper(self, *args, **kwargs)
    424     self._setattr_tracking = False  # pylint: disable=protected-access
    425     try:
--> 426       method(self, *args, **kwargs)
    427     finally:
    428       self._setattr_tracking = previous_value  # pylint: disable=protected-access

~/tensorflow/venv/local/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py in add(self, layer)
    138       raise TypeError('The added layer must be '
    139                       'an instance of class Layer. '
--> 140                       'Found: ' + str(layer))
    141     self.built = False
    142     set_inputs = False

TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.core.Dense object at 0x7f34c47550f0>

追加したのは立派なLayerクラスですが、なぜ文句言われるでしょうか。

例:正常時の動き

model

model = keras.Sequential()
model.add(keras.layers.Dense(8, input_shape=(10, 16))) # [5]
model.add(keras.layers.Dense(32)) # [4]
model.summary()

[4] ここが長い形式なら大丈夫
[5] ちなみに、ここ短い方式もできると思ったらできませんでした。

出力

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_20 (Dense)             (None, 10, 8)             136       
_________________________________________________________________
dense_21 (Dense)             (None, 10, 32)            288       
=================================================================
Total params: 424
Trainable params: 424
Non-trainable params: 0
_________________________________________________________________

原因

SequentialとDenseのimport書き方が同じである必要

例:正し書き方。

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

例:一応大丈夫な書き方。

from keras import Sequential
from keras.layers import Dense

SequentialとDenseのimportがそろっているから大丈夫だが、以下の文句が言われる。

Using TensorFlow backend.

例:ダメな書き方

from tensorflow.keras import Sequential
from keras.layers import Dense

SequentialとDenseのimportが違うルートからダメ。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?