可训练的多参数 Activ。函数 (RBF) NeuPy / Theano
Trainable, Multi-Parameter Activ. Function (RBF) NeuPy / Theano
如何在 Neupy 或 Theano 中实现自定义激活函数(RBF 核,均值和方差由梯度下降调整)以用于 Neupy。
{快速背景知识:梯度下降适用于网络中的每个参数。我想制作一个包含优化特征参数的特殊特征 space 所以 Neupy}
我认为我的问题在于参数的创建、它们的大小以及它们之间的连接方式。
感兴趣的主要功能。
激活函数Class
class RBF(layers.ActivationLayer):
def initialize(self):
super(RBF, self).initialize()
self.add_parameter(name='mean', shape=(1,),
value=init.Normal(), trainable=True)
self.add_parameter(name='std_dev', shape=(1,),
value=init.Normal(), trainable=True)
def output(self, input_value):
return rbf(input_value, self.parameters)
RBF 函数
def rbf(input_value, parameters):
K = _outer_substract(input_value, parameters['mean'])
return np.exp(- np.linalg.norm(K)/parameters['std_dev'])
函数要整形?
def _outer_substract(x, y):
return (x - y.T).T
非常感谢您的帮助,因为这将使您深入了解如何自定义 neupy 网络。文档至少可以在某些领域使用一些工作......
当图层更改输入变量的形状时,它必须将更改通知后续图层。对于这种情况,它必须自定义 output_shape
属性。例如:
from neupy import layers
from neupy.utils import as_tuple
import theano.tensor as T
class Flatten(layers.BaseLayer):
"""
Slight modification of the Reshape layer from the neupy library:
https://github.com/itdxer/neupy/blob/master/neupy/layers/reshape.py
"""
@property
def output_shape(self):
# Number of output feature depends on the input shape
# When layer receives input with shape (10, 3, 4)
# than output will be (10, 12). First number 10 defines
# number of samples which you typically don't need to
# change during propagation
n_output_features = np.prod(self.input_shape)
return (n_output_features,)
def output(self, input_value):
n_samples = input_value.shape[0]
return T.reshape(input_value, as_tuple(n_samples, self.output_shape))
如果你运行它在终端你会看到它有效
>>> network = layers.Input((3, 4)) > Flatten()
>>> predict = network.compile()
>>> predict(np.random.random((10, 3, 4))).shape
(10, 12)
在你的示例中我可以看到一些问题:
rbf
函数没有 return theano 表达式。它应该在函数编译期间失败
- 如果您不指定要计算范数的轴,
np.linalg.norm
等函数将 return 您标量。
以下解决方案应该适合您
import numpy as np
from neupy import layers, init
import theano.tensor as T
def norm(value, axis=None):
return T.sqrt(T.sum(T.square(value), axis=axis))
class RBF(layers.BaseLayer):
def initialize(self):
super(RBF, self).initialize()
# It's more flexible when shape of the parameters
# denend on the input shape
self.add_parameter(
name='mean', shape=self.input_shape,
value=init.Constant(0.), trainable=True)
self.add_parameter(
name='std_dev', shape=self.input_shape,
value=init.Constant(1.), trainable=True)
def output(self, input_value):
K = input_value - self.mean
return T.exp(-norm(K, axis=0) / self.std_dev)
network = layers.Input(1) > RBF()
predict = network.compile()
print(predict(np.random.random((10, 1))))
network = layers.Input(4) > RBF()
predict = network.compile()
print(predict(np.random.random((10, 4))))
虽然 itdxer 已经充分回答了这个问题,但我想添加这个问题的确切解决方案。
建筑创作
network = layers.Input(size) > RBF() > layers.Softmax(num_out)
激活函数
# Elementwise Gaussian (RBF)
def rbf(value, mean, std):
return T.exp(-.5*T.sqr(value-mean)/T.sqr(std))/(std*T.sqrt(2*np.pi))
RBFClass
class RBF(layers.BaseLayer):
def initialize(self):
# Begin by initializing.
super(RBF, self).initialize()
# Add parameters to train
self.add_parameter(name='means', shape=self.input_shape,
value=init.Normal(), trainable=True)
self.add_parameter(name='std_dev', shape=self.input_shape,
value=init.Normal(), trainable=True)
# Define output function for the RBF layer.
def output(self, input_value):
K = input_value - self.means
return rbf(input_value,self.means,self.std_dev
培训
如果您有兴趣参加培训。就这么简单,
# Set training algorithm
gdnet = algorithms.Momentum(
network,
momenutm = 0.1
)
# Train.
gdnet.train(x,y,max_iter=100)
这会使用正确的输入和目标进行编译,并且均值和方差会在逐元素的基础上进行更新。
如何在 Neupy 或 Theano 中实现自定义激活函数(RBF 核,均值和方差由梯度下降调整)以用于 Neupy。
{快速背景知识:梯度下降适用于网络中的每个参数。我想制作一个包含优化特征参数的特殊特征 space 所以 Neupy}
我认为我的问题在于参数的创建、它们的大小以及它们之间的连接方式。
感兴趣的主要功能。
激活函数Class
class RBF(layers.ActivationLayer):
def initialize(self):
super(RBF, self).initialize()
self.add_parameter(name='mean', shape=(1,),
value=init.Normal(), trainable=True)
self.add_parameter(name='std_dev', shape=(1,),
value=init.Normal(), trainable=True)
def output(self, input_value):
return rbf(input_value, self.parameters)
RBF 函数
def rbf(input_value, parameters):
K = _outer_substract(input_value, parameters['mean'])
return np.exp(- np.linalg.norm(K)/parameters['std_dev'])
函数要整形?
def _outer_substract(x, y):
return (x - y.T).T
非常感谢您的帮助,因为这将使您深入了解如何自定义 neupy 网络。文档至少可以在某些领域使用一些工作......
当图层更改输入变量的形状时,它必须将更改通知后续图层。对于这种情况,它必须自定义 output_shape
属性。例如:
from neupy import layers
from neupy.utils import as_tuple
import theano.tensor as T
class Flatten(layers.BaseLayer):
"""
Slight modification of the Reshape layer from the neupy library:
https://github.com/itdxer/neupy/blob/master/neupy/layers/reshape.py
"""
@property
def output_shape(self):
# Number of output feature depends on the input shape
# When layer receives input with shape (10, 3, 4)
# than output will be (10, 12). First number 10 defines
# number of samples which you typically don't need to
# change during propagation
n_output_features = np.prod(self.input_shape)
return (n_output_features,)
def output(self, input_value):
n_samples = input_value.shape[0]
return T.reshape(input_value, as_tuple(n_samples, self.output_shape))
如果你运行它在终端你会看到它有效
>>> network = layers.Input((3, 4)) > Flatten()
>>> predict = network.compile()
>>> predict(np.random.random((10, 3, 4))).shape
(10, 12)
在你的示例中我可以看到一些问题:
rbf
函数没有 return theano 表达式。它应该在函数编译期间失败- 如果您不指定要计算范数的轴,
np.linalg.norm
等函数将 return 您标量。
以下解决方案应该适合您
import numpy as np
from neupy import layers, init
import theano.tensor as T
def norm(value, axis=None):
return T.sqrt(T.sum(T.square(value), axis=axis))
class RBF(layers.BaseLayer):
def initialize(self):
super(RBF, self).initialize()
# It's more flexible when shape of the parameters
# denend on the input shape
self.add_parameter(
name='mean', shape=self.input_shape,
value=init.Constant(0.), trainable=True)
self.add_parameter(
name='std_dev', shape=self.input_shape,
value=init.Constant(1.), trainable=True)
def output(self, input_value):
K = input_value - self.mean
return T.exp(-norm(K, axis=0) / self.std_dev)
network = layers.Input(1) > RBF()
predict = network.compile()
print(predict(np.random.random((10, 1))))
network = layers.Input(4) > RBF()
predict = network.compile()
print(predict(np.random.random((10, 4))))
虽然 itdxer 已经充分回答了这个问题,但我想添加这个问题的确切解决方案。
建筑创作
network = layers.Input(size) > RBF() > layers.Softmax(num_out)
激活函数
# Elementwise Gaussian (RBF)
def rbf(value, mean, std):
return T.exp(-.5*T.sqr(value-mean)/T.sqr(std))/(std*T.sqrt(2*np.pi))
RBFClass
class RBF(layers.BaseLayer):
def initialize(self):
# Begin by initializing.
super(RBF, self).initialize()
# Add parameters to train
self.add_parameter(name='means', shape=self.input_shape,
value=init.Normal(), trainable=True)
self.add_parameter(name='std_dev', shape=self.input_shape,
value=init.Normal(), trainable=True)
# Define output function for the RBF layer.
def output(self, input_value):
K = input_value - self.means
return rbf(input_value,self.means,self.std_dev
培训
如果您有兴趣参加培训。就这么简单,
# Set training algorithm
gdnet = algorithms.Momentum(
network,
momenutm = 0.1
)
# Train.
gdnet.train(x,y,max_iter=100)
这会使用正确的输入和目标进行编译,并且均值和方差会在逐元素的基础上进行更新。