简体   繁体   English

TypeError:添加的层必须是 class 层的实例。 找到:Tensor(“input_1:0”, shape=(None, 64, 64, 3), dtype=float32) -Python

[英]TypeError: The added layer must be an instance of class Layer. Found: Tensor(“input_1:0”, shape=(None, 64, 64, 3), dtype=float32) -Python

I actually try to make a convolutional neural network in order to classify dogs and cat ( i'm newbie in Machine Learning so don't judge me too bad:) ).我实际上尝试制作一个卷积神经网络来对狗和猫进行分类(我是机器学习的新手,所以不要对我评价太差:))。 I got the exactly the same error from the title of this post.我从这篇文章的标题中得到了完全相同的错误。 To be honest, i tried to learn myself from Keras API documentation, tried to catch some tips from stackoverflow,github,towardsdatascience and others.老实说,我试图从 Keras API 文档中学习自己,试图从 stackoverflow、github、towardsdatascience 等中获取一些技巧。 Some said that error belongs to different versions of tensorflow and keras libraries,others said that it belongs to syntax.有人说错误属于 tensorflow 和 keras 库的不同版本,其他人说它属于语法。 I will leave my code here,tell me where i made mistakes,i'm open to learn new tips.我会把我的代码留在这里,告诉我哪里出错了,我愿意学习新的技巧。

#IMPORTING LIBRARIES
import tensorflow as tf
import pandas as pd
import keras
from keras.preprocessing.image import ImageDataGenerator



#IMAGE DATA PREPROCESSING

#preprocessing the training set
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
training_set = train_datagen.flow_from_directory(
        directory = r"C:\Users\Cucu\Downloads\training_set",
        target_size=(64 , 64),
        batch_size=32,
        class_mode='binary')

#preprocessing the test set
test_datagen = ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory(
        directory = r"C:\Users\Cucu\Downloads\test_set",
        target_size=(64 , 64),
        batch_size=32,
        class_mode='binary')


 
#BULDING THE CNN
#
#
#initialising the cnn
cnn = tf.keras.models.Sequential()


#convolution
cnn.add(tf.keras.layers.Conv2D(filters = 32 , kernel_size = 3 , activation = 'relu' ))
cnn.add(keras.Input(shape=(64, 64, 3)))

#pooling
cnn.add(tf.keras.layers.MaxPool2D( pool_size = 2 , strides = 2))


#adding a SECOND CONVOLUTIONAL LAYER
cnn.add(tf.keras.layers.Conv2D(filters = 32 , kernel_size = 3 , activation = 'relu'))
cnn.add(tf.keras.layers.MaxPool2D( pool_size = 2 , strides = 2))


#flattening
cnn.add(tf.keras.layers.Flatten())


#full connection
cnn.add(tf.keras.layers.Dense(units = 128 , activation = 'relu'))


#adding the output layer
cnn.add(tf.keras.layers.Dense(units = 4 , activation = 'sigmoid'))

and the error(exactly as the title) is:并且错误(与标题完全相同)是:

TypeError: The added layer must be an instance of class Layer. Found: Tensor("input_1:0", shape=(None, 64, 64, 3), dtype=float32)

Massive apreciation to those who can give me some tips.非常感谢那些能给我一些提示的人。 I know this is an extra full beginner level,but you know,sometimes you have to learn along the way of practical experience:)我知道这是一个额外的完整初学者级别,但你知道,有时你必须沿着实践经验学习:)

replace:代替:

#convolution
cnn.add(tf.keras.layers.Conv2D(filters = 32 , kernel_size = 3 , activation = 'relu' ))
cnn.add(keras.Input(shape=(64, 64, 3)))

by:经过:

cnn.add(tf.keras.Input(shape=(64, 64, 3)))
cnn.add(tf.keras.layers.Conv2D(filters = 32 , kernel_size = 3 , activation = 'relu' ))

Usually, the Input layer is the first one of your network and you should choose between keras and tf.keras (the second one preferably)通常, Input层是网络的第一个,您应该在kerastf.keras之间进行选择(最好是第二个)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:添加的图层必须是类图层的实例。 找到:Tensor(&quot;concatenate_6/concat:0&quot;, shape=(None, 4608), dtype=float32) - TypeError: The added layer must be an instance of class Layer. Found: Tensor("concatenate_6/concat:0", shape=(None, 4608), dtype=float32) 类型错误:添加的图层必须是类图层的实例。 找到:Tensor(&quot;input_2:0&quot;, shape=(?, 22), dtype=float32) - TypeError: The added layer must be an instance of class Layer. Found: Tensor("input_2:0", shape=(?, 22), dtype=float32) 添加的层必须是 class 层的实例。 找到:张量(“conv1d_12/Relu:0”, shape=(?, 41, 64), dtype=float32) - The added layer must be an instance of class Layer. Found: Tensor(“conv1d_12/Relu:0”, shape=(?, 41, 64), dtype=float32) 图断开连接:无法在“input_1”层获取张量 Tensor("input_1:0", shape=(None, 299, 299, 3), dtype=float32) 的值 - Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 299, 299, 3), dtype=float32) at layer "input_1" raise TypeError(&#39;The added layer must be &#39; TypeError: The added layer must be an instance of class Layer.Found: tf.Tensor - raise TypeError('The added layer must be ' TypeError: The added layer must be an instance of class Layer. Found: tf.Tensor 来自“tf.keras.layers.concatenate”的类型错误:添加的层必须是类层的实例。 发现:张量 - TypeError from "tf.keras.layers.concatenate": The added layer must be an instance of class Layer. Found: Tensor 添加的图层必须是类图层的实例。 成立:<tensorflow.python.keras.engine.input_layer.InputLayer> - The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.engine.input_layer.InputLayer> Tensorflow 2.0 警告-dense_features 正在将输入张量从 dtype float64 转换为 float32 层的 dtype - Tensorflow 2.0 Warnings - dense_features is casting an input tensor from dtype float64 to the layer's dtype of float32 图表断开:无法在“input_5”层获取张量 Tensor(“input_5:0”, shape=(None, None, None, 128), dtype=float32) 的值 - Graph disconnected: cannot obtain value for tensor Tensor(“input_5:0”, shape=(None, None, None, 128), dtype=float32) at layer “input_5” 警告:tensorflow:Layer my_model 正在将输入张量从 dtype float64 转换为 float32 层的 dtype,这是 TensorFlow 2 中的新行为 - WARNING:tensorflow:Layer my_model is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM