1
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 1 year has passed since last update.

Tensorflow.kerasとkeras混ぜてモデル作って死にかけた話

Last updated at Posted at 2021-12-02

モデルを作ろうと思ったきっかけは,ファインチューニングしてみよう!という試みからである.
Kerasとか知らんけどまあ適当に作るか!の気持ち.これが全ての元凶___

#失敗
以下のようにコードを書いてみた.
環境はcondaで作っており,
python==3.7
tensorflow==2.4
である.

test_ng_model.py
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import binary_crossentropy
from keras.layers import Input, Dense, Dropout

#作成したレイヤを順番に保存しておくリスト
layers = []

#入力層の作成
inputs = tf.keras.Input(shape=(3,))
layers.append(inputs)

#全結合層を2層追加
for l in range(2):
    x = Dense(3, activation="sigmoid")(layers[-1])
    layers.append(x)

#出力層の作成
out = Dense(1, activation='sigmoid', name='new_target')(layers[-1])

#モデルの入出力を定義し,作成
new_model = Model(inputs=[inputs], outputs=[out])

#適当にコンパイル
new_model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.001), metrics=[tf.keras.metrics.AUC()])

#モデル構造を出力
new_model.summary()

結果,こんな感じのエラーが吐かれる.

error
Traceback (most recent call last):
  File "test_ng_model.py", line 21, in <module>
    x = Dense(3, activation="sigmoid")(layers[-1])

... #省略

TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

#成功
もうわけがわからず,エラー文をひたすらググり,たどり着いた解決策が以下である

改良点
#元のコード:
from keras.layers import Input, Dense, Dropout

#変更後のコード:
from tensorflow.keras.layers import Input, Dense, Dropout

これだけ.

無事サマリーも出力された.

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 3)]               0         
_________________________________________________________________
dense (Dense)                (None, 3)                 12        
_________________________________________________________________
dense_1 (Dense)              (None, 3)                 12        
_________________________________________________________________
new_target (Dense)           (None, 1)                 4         
=================================================================
Total params: 28
Trainable params: 4
Non-trainable params: 24

tensorflow.kerasとkerasが混在していたのがダメだったっぽい...知らんがな!!!!!!!!!!!!!!

1
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
1
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?