ValueError:形状必须相等,但等级必须为1和0将形状1与其他形状合并表示“损失/增加”

如何解决ValueError:形状必须相等,但等级必须为1和0将形状1与其他形状合并表示“损失/增加”

我正在尝试使用张量流创建变体自动编码器。我已按照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,1))
test.compile('adam',loss=None)
test.summary()
test.train_on_batch(np.random.randn(32,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 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,[].

代码如下所示。

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',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,**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),name='dense2')
        self.reshaped = tfl.Reshape(dense_reshape,name='reshape')

        self.deconvs=[
            ConvolutionalBlock(128,'conv1',deconv=True),'conv2','conv3','conv4',ConvolutionalBlock(8,'conv5',deconv=True)
        ]

        self.output_layer = tfl.Conv2D(filters=1,activation='sigmoid',# check this
                                       padding='same',name='decodedconv',kernel_initializer='he_normal')

    def call(self,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,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,mask=None):
        dense_output,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].

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,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,2].

注意-错误消息的措辞在 tensorflow 2.x

中有所不同

要解决此错误,请将相同秩的张量传递给tf.math.add_n函数。

固定代码-

%tensorflow_version 1.x
import tensorflow as tf

a = tf.constant([[3,8]])
b = tf.constant([[1,6],9]])
tf.math.add_n([a,a])

输出-

<tf.Tensor 'AddN_1:0' shape=(2,2) dtype=int32>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-