Преобразование функции NumPy в тензорный поток - PullRequest
0 голосов
/ 18 октября 2019

Я пытаюсь представить свою пользовательскую активацию для моей нейронной сети. Вот код, который я использовал для этого:

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)

Как вы видите, я пытаюсь реализовать версию функции обратной ошибки (erfinv) TensorFlow в функции myfun ,Кажется, что есть проблема, так как я получаю эту ошибку:

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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...