当前位置: 首页 > 知识库问答 >
问题:

为什么会出现“input_2_1: 0既进又取”的错误?

马臻
2023-03-14

我试图在我的网络中显示中间层的输出,我使用了以下代码:

from keras import models
layer_outputs = [layer.output for layer in w_extraction.layers[:102]] 
activation_model = models.Model(inputs=w_extraction.input, outputs=layer_outputs)
activations = activation_model.predict([x_test[8000:8001],wt_expand])

但它会产生这种错误。我不知道它为什么会产生这种错误!你能帮我解决这个问题吗

回溯(最近一次呼叫最后一次):

File",第1行,在激活=activation_model.predict([x_test[8000:8001],wt_expand])

文件“D:\software\Anaconda3\envs\py36\lib\site packages\keras\engine\training.py”,第1169行,预测步骤=步骤)

文件“D:\software\Anaconda3\envs\py36\lib\site packages\keras\engine\training\u arrays.py”,第294行,在predict\u loop batch\u outs=f(ins\U batch)中

文件“D:\software\Anaconda3\envs\py36\lib\site packages\keras\backend\tensorflow\u backend.py”,第2715行,在call return self中_呼叫(输入)

文件“D:\software\Anaconda3\envs\py36\lib\site packages\keras\backend\tensorflow\u backend.py”,第2671行,在调用会话中)

文件“D:\software\Anaconda3\envs\py36\lib\site packages\keras\backend\tensorflow\u backend.py”,第2623行,位于“make\u callable callable\u fn=session”中_从选项(可调用选项)使可调用

文件"D:\Software\Anaconda3\envs\py36\lib\site-包\tenstorflow\python\Client\session.py",第1471行,_make_callable_from_options返回Base会话。_Callable(自我、callable_options)

文件“D:\software\Anaconda3\envs\py36\lib\site packages\tensorflow\python\client\session.py”,第1425行,在初始化会话中_会话、选项(ptr、状态)

在退出c_api中的文件"D:\Software\Anaconda3\envs\py36\lib\site-包\tenstorflow\python\frame\errors_impl.py",第528行。TF_GetCode(self.status.status))

InvalidArgumentError:input_2_1:0同时被馈送和获取。

我的完整代码如下:

from keras.layers import Input, Concatenate, GaussianNoise,Dropout,BatchNormalization
from keras.layers import Conv2D, AtrousConv2D
from keras.models import Model
from keras.datasets import mnist
from keras.callbacks import TensorBoard
from keras import backend as K
from keras import layers
import matplotlib.pyplot as plt
import tensorflow as tf
import keras as Kr
from keras.optimizers import SGD,RMSprop,Adam
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint
import numpy as np
import pylab as pl
import matplotlib.cm as cm
import keract
from matplotlib import pyplot
from keras import optimizers
from keras import regularizers

from tensorflow.python.keras.layers import Lambda;
#-----------------building w train---------------------------------------------
w_expand=np.zeros((49999,28,28),dtype='float32')
wv_expand=np.zeros((9999,28,28),dtype='float32')
wt_random=np.random.randint(2, size=(49999,4,4))
wt_random=wt_random.astype(np.float32)
wv_random=np.random.randint(2, size=(9999,4,4))
wv_random=wv_random.astype(np.float32)
w_expand[:,:4,:4]=wt_random
wv_expand[:,:4,:4]=wv_random
x,y,z=w_expand.shape
w_expand=w_expand.reshape((x,y,z,1))
x,y,z=wv_expand.shape
wv_expand=wv_expand.reshape((x,y,z,1))

#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,4,4))
w_test=w_test.astype(np.float32)
wt_expand=np.zeros((1,28,28),dtype='float32')
wt_expand[:,0:4,0:4]=w_test
wt_expand=wt_expand.reshape((1,28,28,1))

#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
wtm=Input((28,28,1))
image = Input((28, 28, 1))
conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1e',dilation_rate=(2,2))(image)
conv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2e',dilation_rate=(2,2))(conv1)
conv3 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl3e',dilation_rate=(2,2))(conv2)
BN=BatchNormalization()(conv3)
encoded =  Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I',dilation_rate=(2,2))(BN)


add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,wtm])

#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
deconv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1d',dilation_rate=(2,2))(encoded_merged)
deconv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2d',dilation_rate=(2,2))(deconv1)
deconv3 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl3d',dilation_rate=(2,2))(deconv2)
deconv4 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl4d',dilation_rate=(2,2))(deconv3)
BNd=BatchNormalization()(deconv3)
#DrO2=Dropout(0.25,name='DrO2')(BNd)

decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='decoder_output',dilation_rate=(2,2))(BNd) 
#model=Model(inputs=image,outputs=decoded)

model=Model(inputs=[image,wtm],outputs=decoded)

decoded_noise = GaussianNoise(0.5)(decoded)

#----------------------w extraction------------------------------------
convw1 = Conv2D(64, (3,3), activation='relu', padding='same', name='conl1w',dilation_rate=(2,2))(decoded_noise)
convw2 = Conv2D(64, (3, 3), activation='relu', padding='same', name='convl2w',dilation_rate=(2,2))(convw1)
convw3 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl3w',dilation_rate=(2,2))(convw2)
convw4 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl4w',dilation_rate=(2,2))(convw3)
convw5 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl5w',dilation_rate=(2,2))(convw4)
convw6 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl6w',dilation_rate=(2,2))(convw5)
pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W',dilation_rate=(2,2))(convw6)  
w_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])

#----------------------training the model--------------------------------------
#------------------------------------------------------------------------------
#----------------------Data preparation----------------------------------------

(x_train, _), (x_test, _) = mnist.load_data()
x_validation=x_train[1:10000,:,:]
x_train=x_train[10001:60000,:,:]
#
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_validation = x_validation.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))  # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))  # adapt this if using `channels_first` image data format
x_validation = np.reshape(x_validation, (len(x_validation), 28, 28, 1))

#---------------------compile and train the model------------------------------
w_extraction.compile(optimizer='adam', loss={'decoder_output':'mse','reconstructed_W':'binary_crossentropy'}, loss_weights={'decoder_output': 0.45, 'reconstructed_W': 1.0},metrics=['mae'])
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=20)
#rlrp = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=20, min_delta=1E-4, verbose=1)
mc = ModelCheckpoint('best_model_5x5F_dil_Los751.h5', monitor='val_loss', mode='min', verbose=1, save_best_only=True)
history=w_extraction.fit([x_train,w_expand], [x_train,w_expand],
          epochs=200,
          batch_size=16, 
          validation_data=([x_validation,wv_expand], [x_validation,wv_expand]),
          callbacks=[TensorBoard(log_dir='E:concatnatenetwork', histogram_freq=0, write_graph=False),es,mc])

共有1个答案

施靖
2023-03-14

您不能同时提供和获取占位符(即输入层的基础张量)。有关更多信息,请参阅此答案。因此,您必须将w_extraction模型的输入张量从activation_model的输出中排除。一种方法是根据图层名称对其进行过滤:

layer_outputs = [layer.output for layer in w_extraction.layers[:102] if not layer.name.startswith('input')]
 类似资料:
  • 错误:第 1 行的解析错误:函数搜索(sour ^ 期望“字符串”、“数字”、“空”、“真”、“假”、“{”、“[”,得到“未定义” 代码:

  • 我实现了以下REST调用: 也是一个提供和使用application/x-protobuf的提供者类: 我的pom。xml看起来像: 我的小部件。原型: 选项java_package=“example”;选项java\u outer\u classname=“WidgetsProtoc”; 对于http://localhost:8080/RESTfulExample/widgets我得到以下错误消

  • 问题内容: 我已经阅读了 为什么在尝试在数据库中加载Blob时会出现java.lang.AbstractMethodError?,下载了我能找到的所有11g jdbc驱动程序,并将它们作为库和/或jar文件添加到了我的NetBeans应用程序中。我仍然不断收到相同的AbstractMethodError,这让我很生气!任何指导将不胜感激! 错误信息: 问题答案: 问题的原因是软件不兼容(jar文件

  • 当我试图从netbean部署我的web应用程序时,我得到以下错误: 那是什么?为什么我会犯这个错误? 以下是来自的代码片段: 以下是netbeans服务器设置中的快照:

  • 问题内容: 我正在尝试使用DB2运行一个简单的SQL语句,并且遇到了一些问题。我想在txt / db2文件中有一个脚本,并让引擎处理所有命令 这是脚本: 当我运行db2 -f / pathtofile时,我得到: 我究竟做错了什么?我的脚本有问题吗?另外,为什么没有“;”也能正常工作 我的陈述结尾处的终止符? 谢谢, 问题答案: 可能会有帮助, http://www.uc.edu/R/r25/do

  • 尽管花了几个小时的时间,我还是弄不明白为什么会出现404错误。 我有一个非常简单的spring MVC应用程序,但我真的看不出有什么错误。请帮帮我,我疯了 结构: SpringMVCDemo src公司 视图 main-menu.jsp 我的控制器:HomeController。Java语言 我的视图:主菜单。jsp 网状物xml spring mvc演示servlet。xml