- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 tensorflow 创建一个变分自动编码器。我已按照 keras 网站 ( https://keras.io/guides/making_new_layers_and_models_via_subclassing/ ) 执行了所有步骤
不过我做了一些小改动。
annealing_weight = tf.keras.backend.variable(0.01)
test = VariationalAutoEncoder(annealing_weight,
[8, 8, 128],
input_shape=(None, 256, 256, 1))
test.compile('adam', loss=None)
test.summary()
test.train_on_batch(np.random.randn(32, 256, 256, 1),None)
我能够编译网络并获得摘要。一切似乎都很正常。
WARNING:tensorflow:AutoGraph could not transform <bound method ConvolutionalBlock.call of <__main__.ConvolutionalBlock object at 0x000000001D5DC408>> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: Unable to locate the source code of <bound method ConvolutionalBlock.call of <__main__.ConvolutionalBlock object at 0x000000001D5DC408>>. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code
WARNING:tensorflow:Output output_1 missing from loss dictionary. We assume this was done on purpose. The fit and evaluate APIs will not be expecting any data to be passed to output_1.
Traceback (most recent call last):
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1619, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 1 and 0
From merging shape 1 with other shapes. for 'loss_1/AddN' (op: 'AddN') with input shapes: [?], [?], [], [], [], [], [], [], [], [], [], [], [], [].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 10, in <module>
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1078, in train_on_batch
standalone=True)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 416, in train_on_batch
extract_tensors_from_dataset=True)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2360, in _standardize_user_data
self._compile_from_inputs(all_inputs, y_input, x, y)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2618, in _compile_from_inputs
experimental_run_tf_function=self._experimental_run_tf_function)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\training\tracking\base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 446, in compile
self._compile_weights_loss_and_weighted_metrics()
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\training\tracking\base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1592, in _compile_weights_loss_and_weighted_metrics
self.total_loss = self._prepare_total_loss(masks)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1701, in _prepare_total_loss
math_ops.add_n(custom_losses))
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\util\dispatch.py", line 180, in wrapper
return target(*args, **kwargs)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\ops\math_ops.py", line 3053, in add_n
return gen_math_ops.add_n(inputs, name=name)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\ops\gen_math_ops.py", line 420, in add_n
"AddN", inputs=inputs, name=name)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 742, in _apply_op_helper
attrs=attr_protos, op_def=op_def)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 595, in _create_op_internal
compute_device)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3322, in _create_op_internal
op_def=op_def)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1786, in __init__
control_input_ops)
File "C:\Users\user\.conda\envs\Thesis\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1622, in _create_c_op
raise ValueError(str(e))
ValueError: Shapes must be equal rank, but are 1 and 0
From merging shape 1 with other shapes. for 'loss_1/AddN' (op: 'AddN') with input shapes: [?], [?], [], [], [], [], [], [], [], [], [], [], [], [].
代码如下所示。
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers as tfl
class ConvolutionalBlock(tfl.Layer):
def __init__(self, filters, name, deconv=False, **kwargs):
self.conv_layer = tfl.Conv2DTranspose if deconv else tfl.Conv2D
self.conv_layer = self.conv_layer(filters,
kernel_size=3,
padding='same',
kernel_initializer='he_normal',
kernel_regularizer=tf.keras.regularizers.l2(0.0001),
strides=2,
name='conv')
self.batch_norm = tfl.BatchNormalization(name='de_ennorm')
self.relu = tfl.ReLU(name='en_relu')# + str(index))
super(ConvolutionalBlock, self).__init__(name=name, **kwargs)
def call(self, inputs, **kwargs):
outputs = self.conv_layer(inputs)
outputs = self.batch_norm(outputs)
outputs = self.relu(outputs)
return outputs
class Sampling(tfl.Layer):
def __init__(self, **kwargs):
super(Sampling, self).__init__(name='reparameterization_trick', **kwargs)
def call(self, inputs, training=None, mask=None, **kwargs):
x_mean, x_variance = inputs
return x_mean + tf.keras.backend.exp(0.5 * x_variance) * \
tf.keras.backend.random_normal(shape=(32, 128), mean=0., stddev=1.0)
class Encoder(tfl.Layer):
def __init__(self, **kwargs):
super(Encoder, self).__init__(name='Encoder', **kwargs)
self.convs = [
ConvolutionalBlock(8, 'conv1'),
ConvolutionalBlock(16, 'conv2'),
ConvolutionalBlock(32, 'conv3'),
ConvolutionalBlock(64, 'conv4'),
ConvolutionalBlock(128, 'conv5')
]
self.features = tfl.GlobalAveragePooling2D(name='globaverpool')
self.denserepresentation = tfl.Dense(128, activation='relu', name='Dense1')
self.x_mean = tfl.Dense(128, name='meanvector')
self.x_variance = tfl.Dense(128, name='variancevector')
self.sampling = Sampling()
def call(self, inputs, training=None, mask=None, **kwargs):
outputs = inputs
print(outputs)
for layer in self.convs:
outputs = layer(outputs)
print(outputs)
outputs = self.features(outputs)
print(outputs)
dense_output = self.denserepresentation(outputs)
print(dense_output)
x_mean = self.x_mean(dense_output)
x_variance = self.x_variance(dense_output)
output = self.sampling((x_mean,x_variance))
return output, x_mean, x_variance
class Decoder(tfl.Layer):
def __init__(self,
dense_reshape,
**kwargs):
super(Decoder, self).__init__(name='Decoder', **kwargs)
self.denserepresentation = tfl.Dense(np.prod(dense_reshape),
activation='relu',
kernel_regularizer=tf.keras.regularizers.l2(0.0001),
name='dense2')
self.reshaped = tfl.Reshape(dense_reshape,
name='reshape')
self.deconvs=[
ConvolutionalBlock(128, 'conv1', deconv=True),
ConvolutionalBlock(64, 'conv2', deconv=True),
ConvolutionalBlock(32, 'conv3', deconv=True),
ConvolutionalBlock(16, 'conv4', deconv=True),
ConvolutionalBlock(8, 'conv5', deconv=True)
]
self.output_layer = tfl.Conv2D(filters=1,
kernel_size=3,
activation='sigmoid', # check this
padding='same',
name='decodedconv',
kernel_regularizer=tf.keras.regularizers.l2(0.0001),
kernel_initializer='he_normal')
def call(self, inputs, training=None, mask=None):
outputs = inputs
outputs = self.denserepresentation(outputs)
outputs = self.reshaped(outputs)
for layer in self.deconvs:
outputs = layer(outputs)
outputs = self.output_layer(outputs)
return outputs
class VariationalAutoEncoder(tf.keras.Model):
def __init__(self,
annealing_weight,
dense_reshape,
input_shape,
**kwargs):
super(VariationalAutoEncoder, self).__init__(**kwargs)
self.annealing_weight = annealing_weight # for KL-loss
self.encoder = Encoder()
self.decoder = Decoder(dense_reshape)
self.build(input_shape)
def call(self, inputs, training=None, mask=None):
dense_output, x_mean, x_variance = self.encoder(inputs)
output = self.decoder(dense_output)
kl_loss = - self.annealing_weight * tf.reduce_mean(
x_variance - tf.keras.backend.square(x_mean)
- tf.keras.backend.exp(x_variance) + 1,
axis=-1)
self.add_loss(lambda: kl_loss)
return output
最佳答案
正如错误消息所暗示的,输入张量 tf.math.add_n
函数具有不同的等级。下面我重新创建了您的错误-
重现错误的代码 -
%tensorflow_version 1.x
import tensorflow as tf
a = tf.constant([[3, 5], [4, 8]])
b = tf.constant([[[1, 6]], [[2, 9]]])
tf.math.add_n([a, b, a])
输出 -
TensorFlow 1.x selected.
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1606 try:
-> 1607 c_op = c_api.TF_FinishOperation(op_desc)
1608 except errors.InvalidArgumentError as e:
InvalidArgumentError: Shapes must be equal rank, but are 3 and 2
From merging shape 1 with other shapes. for 'AddN' (op: 'AddN') with input shapes: [2,2], [2,1,2], [2,2].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
9 frames
/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1608 except errors.InvalidArgumentError as e:
1609 # Convert to ValueError for backwards compatibility.
-> 1610 raise ValueError(str(e))
1611
1612 return c_op
ValueError: Shapes must be equal rank, but are 3 and 2
From merging shape 1 with other shapes. for 'AddN' (op: 'AddN') with input shapes: [2,2], [2,1,2], [2,2].
注意 -
中的错误消息措辞不同 tensorflow 2.x
tf.math.add_n
功能。
%tensorflow_version 1.x
import tensorflow as tf
a = tf.constant([[3, 5], [4, 8]])
b = tf.constant([[1, 6], [2, 9]])
tf.math.add_n([a, b, a])
输出 -
<tf.Tensor 'AddN_1:0' shape=(2, 2) dtype=int32>
关于python - ValueError : Shapes must be equal rank, 但是是 1 和 0 来自将形状 1 与其他形状合并。对于 'loss/AddN',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63414587/
我正在尝试找出一种计算排名的方法。现在它只需要每个条目的赢/输的比率,所以例如100 次中,有 99 次获胜,则胜率达到 99%。但如果一个参赛作品在 1 票中赢得 1 票,那么它的获胜排名将是 10
我尝试了以下操作,但它没有对每个类别进行明智的排名。相反,在不考虑类别的情况下对所有记录进行排名。我希望每个类别重新出现排名 select rs.Section,rs.Field1,rs.Field
如何获得在分区更改时重新启动的 RANK?我有这张表: ID Date Value 1 2015-01-01 1 2 2015-01-02 1 1; 关于
由于我们可以使用 row_number() 获得分配的行号如果我们想使用 dense_rank() 在不跳过分区内的任何数字的情况下找到每一行的排名,我们为什么需要rank()功能,我想不出任何用例
我很难搜索可以帮助我构建文本序列(特征)分类器的文档、研究或博客。我拥有的文本序列包含网络日志。 我正在使用 TensorFlow 构建 GRU 模型,并将 SVM 作为分类函数。我在处理张量形状时遇
我遇到了这类错误。 colsys.f:1367.51: 1 NOLD, ALDIF, K, NCOMP, M, MSTAR, 3,DUMM,0)
import tensorflow as tf x = [[1,2,3],[4,5,6]] y = [0,1] z = [1,2] x = tf.constant(x) y = tf.constant
我在学习 SQL 中的排名函数,发现它使用的排名与 pandas 方法不同。如何得到相同的答案? 提问链接:https://www.windowfunctions.com/questions/rank
在 SQL Server 数据库中,我有一个我对排名感兴趣的值表。 当我执行 RANK() OVER (ORDER BY VALUE DESC) 作为 RANK 时,我得到以下结果(在假设表中): R
我有一个包含以下字段的游戏 table : ID Name Email Points ---------------------------------- 1 Jo
我有以下 TensorFlow 代码: layer_1 = tf.add(tf.matmul(tf.cast(x, tf.float32), weights['h1']), biases['b1'])
我是 Sentdex 教程的神经网络新手。我尝试运行该代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist i
我是 tensorflow 的新手,我正在尝试将双向 LSTM 的一些代码从旧版本的 tensorflow 更新到最新版本 (1.0),但我收到此错误: Shape must be rank 2 bu
我正在使用以下格式的数据集: Column 1 (What I Have), Column 2 (What I need to see) 8 1 8 1 8 1 9 2 9
我有一个 Keras 函数模型(具有卷积层的神经网络),它可以很好地与 tensorflow 配合使用。我可以运行它,我可以适应它。 但是,使用tensorflow gpu时无法建立模型。 这是构建模
MPI 中的进程以什么顺序执行?我的意思是排名明智的顺序? 例如:rank == 0 首先,rank == 1 接下来? 我通过在运行时给出以下命令来考虑两个过程: mpirun -np 2 示例。
我正在尝试使用 cvxpy(因此使用 cvxopt)在具有 28 个节点和 37 条线路的相对简单的网络中对最佳功率流进行建模,但得到的是“Rank(A) < p or Rank([G; A] ) <
我是 tensorflow 的新手,我正在做一些在线练习以熟悉 tensorflow。我要执行以下任务: Create two tensors x and y of shape 300 from an
我有一个 Ubuntu 对话语料库的 .tfrecords 文件。我正在尝试读取整个数据集,以便我可以将上下文和话语分成几批。使用 tf.parse_single_example 我能够阅读一个示例。
实际上我们不能在 if 语句中使用 tf.var 作为 bool 来代替使用 tf.cond。我为规范化输入数据编写了这段代码,但出现了令人困惑的错误,我哪里做错了? def global_co
我是一名优秀的程序员,十分优秀!