[英]How do I add noise to the weights when calculating the loss with Keras?
[英]How to use weights of a keras layer in calculating loss function?
我正在尝试构建一个只有一层的自动编码器:
from keras import backend as K
def cost2(y_true, y_pred):
print "shapes:", model.get_weights()[0].shape
yy = K.dot( y_pred, model.get_weights()[0].T )
return np.sum((y_true - yy)**2)
x = Input(shape=(original_dim,))
y = Dense(latent_dim)(x)
model = Model(inputs=x, outputs=y)
model.summary()
model.compile(optimizer='adagrad', loss=cost2)
这给了我错误:
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 784) 0
_________________________________________________________________
dense_1 (Dense) (None, 2) 1570
=================================================================
Total params: 1,570
Trainable params: 1,570
Non-trainable params: 0
_________________________________________________________________
shapes: (784, 2)
回溯(最近一次调用):文件“vae_kears_gidital_mnist3.py”,第 45 行,在 model.compile(optimizer='adagrad', loss=cost2) 文件“/Users/asgharrazavi/anaconda/lib/python2.7/site-包/keras/engine/training.py”,第830行,在编译sample_weight,掩码中)文件“/Users/asgharrazavi/anaconda/lib/python2.7/site-packages/keras/engine/training.py”,第429行, in weighted score_array = fn(y_true, y_pred) File "vae_kears_gidital_mnist3.py", line 18, in cost2 yy = K.dot(y_pred, model.get_weights()[0].T ) File "/Users/asgharrazavi/anaconda /lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1048, in dot if ndim(x) is not None and (ndim(x) > 2 or ndim(y) > 2):文件“/Users/asgharrazavi/anaconda/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py”,第 606 行,在 ndim dims = x.get_shape()._dims AttributeError: 'numpy.ndarray' object没有属性“get_shape”
我只是试图将模型的输出乘以模型的转置权重以返回输入维度。 有任何想法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.