简体   繁体   English

Tensorflow:形状必须等于等级,但对于tf.norm来说形状必须为4和1,

[英]Tensorflow : Shapes must be equal rank, but are 4 and 1, for tf.norm

I'm trying to find the norm all the of filters in a conv2d layer. 我试图在conv2d层中找到所有过滤器的标准。 Please find the code for the same below 请在下面找到相同的代码

conv1 = tf.layers.conv2d(
                         inputs=input_layer,
                         filters=32,
                         strides=(1, 1),
                         kernel_size=[3, 3],
                         padding="valid",
                         activation=tf.nn.relu,
                         use_bias=True,
                         kernel_regularizer=tf.nn.l2_loss,
                         bias_regularizer=tf.nn.l2_loss,
                         name="conv1")

var = [v for v in tf.trainable_variables() if "conv1" in v.name]
print(tf.norm(var,axis=4))

Shapes must be equal rank, but are 4 and 1 From merging shape 0 with other shapes. 形状必须相等,但必须为4和1。将形状0与其他形状合并。 for 'norm/packed' (op: 'Pack') with input shapes: [3,3,3,32], [32]. 输入形状为[3,3,3,32],[32]的“标准/打包”(op:“打包”)。

I have tried with multiple axis values from "None to 4" and none work. 我尝试使用从“无到4”的多个轴值,但没有任何效果。 Can someone explain what is the problem and how can it be solved? 有人可以说明问题是什么以及如何解决?

There are two bugs in your code. 您的代码中有两个错误。 One is that var contains a list of tensors while tf.norm() expects just one tensor. 一个是var包含一个张量列表,tf.norm()只期望一个张量。 Furthermore, the weights have a dimension of 4 and dimensions are numbered from 0, so the fourth dimension's axis is 3. This code (tested): 此外,权重的尺寸为4 ,尺寸从0开始编号,因此第四个尺寸的轴为3。此代码(经过测试):

import tensorflow as tf

input_layer = tf.random_uniform( shape = ( 2, 10, 10, 2 ) )

conv1 = tf.layers.conv2d(
                         inputs=input_layer,
                         filters=32,
                         strides=(1, 1),
                         kernel_size=[3, 3],
                         padding="valid",
                         activation=tf.nn.relu,
                         use_bias=True,
                         kernel_regularizer=tf.nn.l2_loss,
                         bias_regularizer=tf.nn.l2_loss,
                         name="conv1")

var = [v for v in tf.trainable_variables() if "conv1" in v.name][ 0 ]
print( tf.norm( var, axis = 3 ) )

will output: 将输出:

Tensor("norm/Squeeze:0", shape=(3, 3, 2), dtype=float32) Tensor(“范数/压缩:0”,shape =(3,3,2),dtype = float32)

with no error. 没有错误。

暂无
暂无

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

相关问题 tf.norm错误ValueError:“ ord”必须是支持的矢量范数,往返 - tf.norm error ValueError: 'ord' must be a supported vector norm, got fro 形状必须相等 - Shapes must be equal rank tf.gradients: ValueError: Shapes must be equal rank, but are 2 and 1 - tf.gradients: ValueError: Shapes must be equal rank, but are 2 and 1 Keras LSTM TensorFlow错误:“形状必须等于等级,但必须为1和0” - Keras LSTM TensorFlow Error: 'Shapes must be equal rank, but are 1 and 0' Tensorflow LSTM错误(ValueError:形状必须等于等级,但为2和1) - Tensorflow LSTM Error (ValueError: Shapes must be equal rank, but are 2 and 1 ) Tensorflow错误:ValueError:形状必须等于等级,但是2和1从形状1与其他形状合并 - Tensorflow Error: ValueError: Shapes must be equal rank, but are 2 and 1 From merging shape 1 with other shapes tensorflow ValueError:两个形状的尺寸0必须相等 - tensorflow ValueError: Dimension 0 in both shapes must be equal tensor_scatter_nd_update ValueError: Shapes must be equal rank, but are 0 and 1 - tensor_scatter_nd_update ValueError: Shapes must be equal rank, but are 0 and 1 ValueError: Shapes must be equal rank, but are 1 and 0 从将形状 1 与其他形状合并。 对于“损失/添加” - ValueError: Shapes must be equal rank, but are 1 and 0 From merging shape 1 with other shapes. for 'loss/AddN' Tensorflow Addons R2 ValueError:两个形状中的尺寸 0 必须相等,但 1 和 5 - Tensorflow Addons R2 ValueError: Dimension 0 in both shapes must be equal, but are 1 and 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM