将numpy函数转换为张量流

时间:2019-10-18 00:09:50

标签: python python-3.x tensorflow keras

我正在尝试为我的神经网络引入自定义激活。这是我用来执行此操作的代码:

def myfun(y):
    import tensorflow as tf
    from scipy.special import erfinv
    return tf.compat.v2.numpy_function(erfinv ,[y],tf.float32)

import numpy as np
from keras import optimizers
from tensorflow.keras.layers import Input, Dense, Activation
from tensorflow.keras.models import Model, Sequential
from keras.backend import tf


def custom_activation3(x):
    from tensorflow.keras import backend as K
    result = myfun( x )

    return result

x_train = np.random.standard_normal(size=(10, 12))

model = Sequential([
         Dense(4, input_shape=(12,)),
         Activation(custom_activation3),
         Dense(12),
         Activation('linear')
])

model.compile(optimizer='adam',loss='mean_squared_error')
model.fit(x_train, x_train, epochs=5,batch_size=None)

如您所见,我正在尝试在函数 myfun 中实现TensorFlow版本的反错误函数(erfinv)。自从出现此错误以来,似乎有一个问题:

WARNING: Logging before flag parsing goes to stderr.
W1017 16:58:53.121699 13896 deprecation.py:506] From C:\Users\r.jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\ops\init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
Traceback (most recent call last):
  File "C:/p/CE/mytest.py", line 28, in <module>
    Activation('linear')
  File "C:\Users\r.jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\r.jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\sequential.py", line 110, in __init__
    self.add(layer)
  File "C:\Users\r.jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\r.jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\sequential.py", line 192, in add
    output_tensor = layer(self.outputs[0])
  File "C:\Users\r.jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\base_layer.py", line 586, in __call__
    self.name)
  File "C:\Users\r.jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\input_spec.py", line 111, in assert_input_compatibility
    layer_name + ' is incompatible with the layer: '
ValueError: Input 0 of layer dense_1 is incompatible with the layer: its rank is undefined, but the layer requires a defined rank.

0 个答案:

没有答案