- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个简单的卷积网络(自动编码器),我想将我的模型分为编码器和解码器两部分。在编码器和解码器之间,我将随机图像添加到编码器的输出,然后将结果发送到解码器部分,但是当我想从编码器到解码器创建模型时,它会产生以下错误:
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_2:0", shape=(?, 28, 28, 1), dtype=float32) at layer "input_2". The following previous layers were accessed without issue: []
当我想要创建解码器模型时,产生了这个错误。我无法理解为什么会产生这个错误。请帮我解决这个错误。
from keras.layers import Input, Concatenate, GaussianNoise,Dropout
from keras.layers import Conv2D
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
import numpy as np
import pylab as pl
import matplotlib.cm as cm
#-----------------building w train---------------------------------------------
w_main = np.random.randint(2,size=(1,4,4,1))
w_main=w_main.astype(np.float32)
w_expand=np.zeros((1,28,28,1),dtype='float32')
w_expand[:,0:4,0:4]=w_main
w_expand.reshape(1,28,28,1)
w_expand=np.repeat(w_expand,49999,0)
#-----------------building w validation---------------------------------------------
w_valid = np.random.randint(2,size=(1,4,4,1))
w_valid=w_valid.astype(np.float32)
wv_expand=np.zeros((1,28,28,1),dtype='float32')
wv_expand[:,0:4,0:4]=w_valid
wv_expand.reshape(1,28,28,1)
wv_expand=np.repeat(wv_expand,9999,0)
#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,4,4,1))
w_test=w_test.astype(np.float32)
wt_expand=np.zeros((1,28,28,1),dtype='float32')
wt_expand[:,0:4,0:4]=w_test
wt_expand.reshape(1,28,28,1)
wt_expand=np.repeat(wt_expand,10000,0)
#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
wtm=Input((28,28,1))
image = Input((28, 28, 1))
conv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1e')(image)
conv2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2e')(conv1)
conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e')(conv2)
DrO1=Dropout(0.25)(conv3)
encoded = Conv2D(1, (3, 3), activation='relu', padding='same',name='reconstructed_I')(DrO1)
#-----------------------adding w---------------------------------------
#add_const = Kr.layers.Lambda(lambda x: x + Kr.backend.constant(w_expand))
#encoded_merged=Kr.layers.Add()([encoded,wtm])
add_const = Kr.layers.Lambda(lambda x: x + wtm)
encoded_merged = add_const(encoded)
encoder=Model(inputs=image, outputs=encoded_merged)
encoder.summary()
#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
#encoded_merged = Input((28, 28, 2))
deconv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1d')(encoded_merged)
deconv2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2d')(deconv1)
deconv3 = Conv2D(8, (3, 3), activation='relu',padding='same', name='convl3d')(deconv2)
DrO2=Dropout(0.25)(deconv3)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same', name='decoder_output')(DrO2)
decoder=Model(inputs=encoded_merged, outputs=decoded)
#decoder.summary()
新代码:
from keras.layers import Input, Concatenate, GaussianNoise,Dropout
from keras.layers import Conv2D
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
import numpy as np
import pylab as pl
import matplotlib.cm as cm
import keract
from keras import optimizers
from keras import regularizers
from keras.callbacks import EarlyStopping
from tensorflow.python.keras.layers import Lambda;
#-----------------building w train---------------------------------------------
w_main = np.random.randint(2,size=(1,14,14,1))
w_main=w_main.astype(np.float32)
w_expand=np.zeros((1,28,28,1),dtype='float32')
w_expand[:,0:14,0:14]=w_main
w_expand.reshape(1,28,28,1)
w_expand=np.repeat(w_expand,49999,0)
#-----------------building w validation---------------------------------------------
w_valid = np.random.randint(2,size=(1,14,14,1))
w_valid=w_valid.astype(np.float32)
wv_expand=np.zeros((1,28,28,1),dtype='float32')
wv_expand[:,0:14,0:14]=w_valid
wv_expand.reshape(1,28,28,1)
wv_expand=np.repeat(wv_expand,9999,0)
#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,14,14,1))
w_test=w_test.astype(np.float32)
wt_expand=np.zeros((1,28,28,1),dtype='float32')
wt_expand[:,0:14,0:14]=w_test
wt_expand.reshape(1,28,28,1)
#wt_expand=np.repeat(wt_expand,10000,0)
#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
wtm=Input((28,28,1))
image = Input((28, 28, 1))
conv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1e')(image)
conv2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2e')(conv1)
conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e')(conv2)
#conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e', kernel_initializer='Orthogonal',bias_initializer='glorot_uniform')(conv2)
DrO1=Dropout(0.25)(conv3)
encoded = Conv2D(1, (3, 3), activation='relu', padding='same',name='reconstructed_I')(DrO1)
#-----------------------adding watermark---------------------------------------
#add_const = Kr.layers.Lambda(lambda x: x + Kr.backend.constant(w_expand))
#encoded_merged=Kr.layers.Add()([encoded,wtm])
add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,wtm])
encoder=Model(inputs=[image,wtm], outputs= encoded_merged)
encoder.summary()
#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
deconv_input=Input((28,28,1))
#encoded_merged = Input((28, 28, 2))
deconv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1d',kernel_regularizer=regularizers.l2(0.001), kernel_initializer='Orthogonal')(deconv_input)
deconv2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2d')(deconv1)
deconv3 = Conv2D(8, (3, 3), activation='relu',padding='same', name='convl3d')(deconv2)
DrO2=Dropout(0.25)(deconv3)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same', name='decoder_output')(DrO2)
decoder=Model(inputs=deconv_input, outputs=decoded)
#decoder.summary()
encoded_merged = encoder([image,wtm])
decoded = decoder(encoded_merged)
model=Model(inputs=[image,wtm],outputs=decoded)
#----------------------w extraction------------------------------------
convw1 = Conv2D(16, (3,3), activation='relu', padding='same', name='conl1w',kernel_regularizer=regularizers.l2(0.001), kernel_initializer='Orthogonal')(decoded)
convw2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='convl2w')(convw1)
convw3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='conl3w')(convw2)
DrO3=Dropout(0.25)(convw3)
pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W')(DrO3)
# reconsider activation (is W positive?)
# should be filter=1 to match W
watermark_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])
#----------------------training the model--------------------------------------
#------------------------------------------------------------------------------
#----------------------Data preparesion----------------------------------------
(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------------------------------
# is accuracy sensible metric for this model?
adadelta=optimizers.Adadelta(lr=1.0,decay=1/1000)
watermark_extraction.compile(optimizer=adadelta, loss={'decoder_output':'mse','reconstructed_W':'mse'}, metrics=['mae'])
watermark_extraction.fit([x_train,w_expand], [x_train,w_expand],
epochs=10,
batch_size=32,
validation_data=([x_validation,wv_expand], [x_validation,wv_expand]),
callbacks=[TensorBoard(log_dir='E:/tmp/AutewithW200', histogram_freq=0, write_graph=False),EarlyStopping(monitor='val_loss', patience=10,min_delta=0)])
model.summary()
新错误:
ValueError: Unknown entry in loss dictionary: "decoder_output". Only expected the following keys: ['model_14', 'reconstructed_W']
最佳答案
当您将张量放入公式中而不是将张量传递到层时,Lambda
层正在攻击系统。
add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,wtm])
或者简单地说:
encoded_merged = Add()([encoded,wtm])
您必须使 wtm
成为模型的输入:
encoder = Model(inputs=[image,wtm], outputs = encoded_merged)
<小时/>
模型应该从输入张量开始,而不是从图中间的张量开始:
deconv_inputs = Input(shape_of_encoded_merged)
deconv1 = Conv2D(16, (3, 3), activation='relu', padding='same', name='convl1d')(deconv_inputs)
....
decoder = Model(inputs=deconv_inputs, outputs=decoded)
<小时/>
然后您可以创建自动编码器:
wtm=Input((28,28,1))
image = Input((28, 28, 1))
encoded_merged = encoder([image,wtm])
decoded = decoder(encoded_merged)
autoencoder = Model([image,wtm], decoded)
关于python - 解决自动编码器模型制作过程中的图形断开错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54409650/
我有课 class Header { @FCBTag(type="type1") --My custom annotation int a = "valueA"; @FCBTa
我一直在使用 Apache MINA 并正在学习 Netty。我过去使用过 MINA 累积编码器/解码器,我有兴趣看看 Netty 是否有类似的功能。我查看了 API,但没有看到任何内容。 最佳答案
我有一组使用 wsdl2java (Axis 1.4) 创建的类,我正在寻找一种方法来解码和编码来自/到字符串和对象的数据。我已经编写了一个 JAXB 解码器,它适用于我们的一些较新的内部对象,因为我
在我的自定义类 WLNetworkClient 中,我必须实现这样的方法: required init(coder aDecoder: NSCoder) { fatalError("init(
基于 transformer 的编码器-解码器模型是 表征学习 和 模型架构 这两个领域多年研究成果的结晶。本文简要介绍了神经编码器-解码器模型的历史,更多背景知识,建议读者阅读由 Seba
在使用 FFMPEG android java 库时发生以下异常的视频播放速度(使视频变慢)。 [aac @ 0x416c26f0] The encoder 'aac' is experimental
我正在从一个程序运行 ffmpeg,我们自己构建了 ffmpeg(我们没有使用包管理器或预构建的东西安装它)。 这是构建的命令: 2020-07-31 12:14:11.942 INFO ffmpeg
许多基于LSTM的seq2seq编码器-解码器架构教程(例如英法翻译),将模型定义如下: encoder_inputs = Input(shape=(None,)) en_x= Embedding(
如何覆盖使用 marshmallow 的 JSON 编码器库,以便它可以序列化 Decimal字段?我想我可以通过覆盖 json_module 来做到这一点在基地Schema或 Meta课,但我不知道
在我的 Grails 2.5.0 应用程序中,我使用了一组自定义 JSON 编码器来严格控制由我的 REST 端点返回的 JSON 格式。目前我在这样的服务中注册这些编码器 class Marshal
我需要多个自定义 JSON 编码器,因为我想针对不同的目的以不同的方式进行编码。我知道如何使用以下方法设置自定义编码器应用程序: JSON.registerObjectMarshaller(MyCla
查看文档,它是这样说的: https://netty.io/4.0/api/io/netty/channel/ChannelPipeline.html A user is supposed to ha
我希望为以下案例类提供 JSON 编码器: import io.circe.generic.extras.Configuration final case class Hello[T]( so
我正在构建一个 JPEG 图像编码器。就目前情况而言,为了对图像进行编码,用户输入他们希望编码的文件的名称以及由此创建的文件的名称。 我希望用户能够在命令行中设置编码的质量。我尝试重命名 new Jp
我有想要在 webview 中显示的 html 文本。 如specification ,数据必须经过 URI 转义。所以我尝试使用 URLEncoder.encode() 函数,但这对我没有帮助,因为
我目前正在自己实现 PNG 滤镜。我正在使用神经网络尝试创建比当前现有的 PNG 过滤器更好的预测: 0 - 无 1 - 子 2 - 向上 3 - 平均 4 - 派斯 5 - 我的实现(使用神经网
让我们假设我们有与 Schema 一致的 XML 和带有一些公共(public)字段的 Java 类: public clas
在我的 Java 应用程序中,我正在寻找 URLEncoder.encode(String s, String enc) 的流媒体版本.我想使用“application/x-www-form-urle
我确实有一个对象层次结构,我想使用“import javax.xml.bind.Marshaller”将其从 Java 对象转换为 xml。我的java类文件被编码在“Cp1252”中,我无法更改它。
使用 Netty 4.0.27 和 Java 1.8.0_20 所以我试图通过构建一个简单的聊天服务器(我猜是典型的网络教程程序?)来了解 Netty 的工作原理。设计我自己的简单协议(protoco
我是一名优秀的程序员,十分优秀!