python - Tensorflow 和 Tensorflow-Probability NotFoundError:没有算法有效![操作:Conv2D]
问题描述
使用 tensorflow 版本 2.4.1 和 tensorflow-probability 0.12.1
我正在尝试实现一个混合密度网络,该网络获取 2D 图像并将它们转换为估计一维输出的密度,并在输出分布上进行正则化分布,以惩罚偏离先验分布的情况。
import tensorflow_probability as tfp
import tensorflow as tf
tfd = tfp.distributions
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Input, Dropout
import numpy as np
data_x = np.random.normal(size(100,32,14,1))
data_y = np.random.normal(size=(100,4))
我将模型实现为继承类:
@tf.function
def nnelu(input):
""" Computes the Non-Negative Exponential Linear Unit"""
return tf.add(tf.constant(1, dtype=tf.float32), tf.nn.elu(input))
class BNN(tf.keras.Model):
def __init__(self, num_components=5,regularizing_distribution=None):
super(BNN, self).__init__()
self.num_components = num_components
self.regularizer = regularizing_distribution
self.conv1 = Conv2D(16,
(3,3),
activation='relu',
padding='same',
name='convolution_1')
self.dropout1 = Dropout(.5)
self.conv2 = Conv2D(32,
(3,3),
activation='relu',
padding='same',
name='convolution_2')
self.dropout2 = Dropout(.5)
self.conv3 = Conv2D(64,
(3,3),
activation='relu',
padding='same',
name='convolution_3')
self.dropout3 = Dropout(.5)
self.dense = Dense(32,activation='relu',name='dense_layer')
self.flatten = Flatten()
self.mu = Dense(self.num_components,name='mixture_means',activation='linear')
self.sigma = Dense(self.num_components,name='mixture_std',activation=nnelu)
self.p = Dense(self.num_components,name='mixing_weights',activation='softmax')
self.reg = Dense(3,name='regularizers',activation='softmax')
def call(self, inputs):
inputs_, outputs = inputs
conv1 = self.conv1(inputs_)
dropout1 = self.dropout1(conv1)
conv2 = self.conv2(dropout1)
dropout2 = self.dropout2(conv2)
conv3 = self.conv3(dropout2)
dropout3 = self.dropout3(conv3)
dense = self.dense(dropout3)
flatten = self.flatten(dense)
alpha = self.p(flatten)
params = [self.mu(flatten),self.sigma(flatten),alpha]
mixture = tfp.layers.DistributionLambda(make_distribution_fn=lambda params:tfd.MixtureSameFamily(
mixture_distribution=tfd.Categorical(probs=params[2]),
components_distribution=tfd.Normal(
loc=params[0],
scale=params[1]
)
),
activity_regularizer=tfp.layers.KLDivergenceRegularizer(distribution_b=self.regularizer,
use_exact_kl=False,
test_points_reduce_axis=(),
test_points_fn=tf.convert_to_tensor,
weight=None)
)(params)
regularizer_alpha = self.reg(flatten)
return mixture.log_prob(outputs), mixture.mean(), regularizer_alpha, alpha
我将损失函数和度量排除在外,因为问题发生在预测步骤。
from tensorflow.keras import Model
input_x = Input(shape=(32,14,1)) #input X values / grid
input_y = Input(shape=(1,)) #input Y values / true values from observation
outputs = BNN(5, regularizing_distribution)([input_x,input_y])
model = Model(inputs=[input_x,input_y],outputs=outputs)
错误发生在调用和预测步骤中。如果我这样做,model([data_x,data_y[:,0]])
或者model.predict([data_x,data_y[:,0]])
我收到以下错误:
---------------------------------------------------------------------------
NotFoundError Traceback (most recent call last)
<ipython-input-141-ed6305fb6a12> in <module>
----> 1 model([data_x,data_y])
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
1010 with autocast_variable.enable_auto_cast_variables(
1011 self._compute_dtype_object):
-> 1012 outputs = call_fn(inputs, *args, **kwargs)
1013
1014 if self._activity_regularizer:
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py in call(self, inputs, training, mask)
422 a list of tensors if there are more than one outputs.
423 """
--> 424 return self._run_internal_graph(
425 inputs, training=training, mask=mask)
426
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py in _run_internal_graph(self, inputs, training, mask)
558
559 args, kwargs = node.map_arguments(tensor_dict)
--> 560 outputs = node.layer(*args, **kwargs)
561
562 # Update tensor_dict.
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
1010 with autocast_variable.enable_auto_cast_variables(
1011 self._compute_dtype_object):
-> 1012 outputs = call_fn(inputs, *args, **kwargs)
1013
1014 if self._activity_regularizer:
<ipython-input-138-b362925fd379> in call(self, inputs)
39 def call(self, inputs):
40 inputs_, outputs = inputs
---> 41 conv1 = self.conv1(inputs_)
42 dropout1 = self.dropout1(conv1)
43 conv2 = self.conv2(dropout1)
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
1010 with autocast_variable.enable_auto_cast_variables(
1011 self._compute_dtype_object):
-> 1012 outputs = call_fn(inputs, *args, **kwargs)
1013
1014 if self._activity_regularizer:
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/keras/layers/convolutional.py in call(self, inputs)
246 inputs = array_ops.pad(inputs, self._compute_causal_padding(inputs))
247
--> 248 outputs = self._convolution_op(inputs, self.kernel)
249
250 if self.use_bias:
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 """Call target, and fall back on dispatchers if there is a TypeError."""
200 try:
--> 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/ops/nn_ops.py in convolution_v2(input, filters, strides, padding, data_format, dilations, name)
1011 dilations=None,
1012 name=None):
-> 1013 return convolution_internal(
1014 input, # pylint: disable=redefined-builtin
1015 filters,
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/ops/nn_ops.py in convolution_internal(input, filters, strides, padding, data_format, dilations, name, call_from_convolution, num_spatial_dims)
1141 op = conv1d
1142
-> 1143 return op(
1144 input,
1145 filters,
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/ops/nn_ops.py in _conv2d_expanded_batch(input, filters, strides, padding, data_format, dilations, name)
2595 # We avoid calling squeeze_batch_dims to reduce extra python function
2596 # call slowdown in eager mode. This branch doesn't require reshapes.
-> 2597 return gen_nn_ops.conv2d(
2598 input,
2599 filter=filters,
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/ops/gen_nn_ops.py in conv2d(input, filter, strides, padding, use_cudnn_on_gpu, explicit_paddings, data_format, dilations, name)
930 return _result
931 except _core._NotOkStatusException as e:
--> 932 _ops.raise_from_not_ok_status(e, name)
933 except _core._FallbackException:
934 pass
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
6860 message = e.message + (" name: " + name if name is not None else "")
6861 # pylint: disable=protected-access
-> 6862 six.raise_from(core._status_to_exception(e.code, message), None)
6863 # pylint: enable=protected-access
6864
/hpc/apps/pyhpc/dist/conda/x86_64/envs/cuda-11.0/lib/python3.8/site-packages/six.py in raise_from(value, from_value)
NotFoundError: No algorithm worked! [Op:Conv2D]
这个错误对我来说有点神秘,它特别令人困惑的原因是,当实现为层类并在层类中实现时,我可以使用 model.fit,但不能使用 model.predict,但我可以使用模型类'也不用。
我尝试选择模型的组件,减少它,以便输出不会作为输入传递;如果列表解包是代码分解的地方。
任何帮助,将不胜感激。
解决方案
推荐阅读
- javascript - d3源代码中带括号的赋值
- java - 当类路径中存在 eclipse moxy jar 时,不考虑 jersey jackson 提供程序
- reactjs - 在路由上显示登录组件,但如果用户不存在,也会显示
- hive - 为什么插入hdfs hive分区表时速度太慢?
- php - 速记如果疯狂,是否可以更聪明地重写?
- c# - 如何使用私钥使用 rsa-sha256 对字符串进行签名?
- java - 将简单 json 字符串中所有项目的键和值打印到控制台
- java - 如何在不进行任何修改的情况下从 Java 中的 Rabbit 接收原始 json 字符串?
- indexing - AEM : 房地产指数
- download - 下载带有依赖项的巧克力包,但稍后安装