如何解决“ TypeError:添加的图层必须是类Layer的实例。”在Python中

时间:2019-03-28 22:46:29

标签: python python-3.x keras

我写了那种神经网络的小东西。问题是我经常收到该错误消息:

"Traceback (most recent call last):
  File "C:/Users/Pigeonnn/PycharmProjects/Noss/Network.py", line 21, in <module>
    model.add(keras.layers.InputLayer(input_shape))
  File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 442, in _method_wrapper
    method(self, *args, **kwargs)
  File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 145, in add
    'Found: ' + str(layer))
TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.input_layer.InputLayer object at 0x0000015EDB394DA0>"

这是我的代码:

import keras
import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.utils import shuffle
import tensorflow as tf

seed = 10
np.random.seed(seed)

dataset = np.loadtxt("dataset2.csv",delimiter=',',skiprows=1)
dataset = shuffle(dataset)

X = dataset[:,2:]
Y = dataset[:,1]

(X_train,X_test,Y_train,Y_test) = train_test_split(X, Y, test_size=0.15, random_state=seed)
input_shape = (13,)

model = tf.keras.models.Sequential()
model.add(keras.layers.InputLayer(input_shape))
model.add(keras.layers.core.Dense(128, activation='relu'))
model.add(keras.layers.core.Dense(128, activation='relu'))
model.add(keras.layers.core.Dense(4, activation='sigmoid'))

model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])

model.fit(X_train,Y_train,epochs=20)

编辑:经过一些调整(更改损失函数,删除tf模型)后,我又遇到了一个错误,这次是:

Traceback (most recent call last):
  File "C:/Users/Pigeonnn/PycharmProjects/Noss/Network.py", line 28, in 
    model.fit(X_train,Y_train,epochs=20)
  File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 952, in fit
    batch_size=batch_size)
  File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 789, in _standardize_user_data
    exception_prefix='target')
  File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training_utils.py", line 138, in standardize_input_data
    str(data_shape))
ValueError: Error when checking target: expected dense_3 to have shape (4,) but got array with shape (1,)

1 个答案:

答案 0 :(得分:0)

您正在使用{strong>不兼容的getAuzrdddeToken(): Observable<any> { const url = 'https://login.microsoftonline.com/' + environment.adalConfigGraph.orgID + '/oauth2/v2.0/token'; const headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'}); const body = new HttpParams(); body.set('client_id', environment.adalConfigGraph.clientID); body.set('grant_type', 'client_credentials'); body.set('scope', 'https://graph.microsoft.com/.default'); body.set('client_secret', environment.adalConfigGraph.secret); return this._http.post(url, body, {headers: headers}).pipe( tap(data => console.log('======== Token: ' + JSON.stringify(data))), ); } tf.keras模块。只使用一个并保持一致。