【发布时间】:2014-02-14 01:09:22
【问题描述】:
我正在构建一个几何神经网络,并且遇到了矢量化问题。基本上,我定义了一个 lambda 函数,它应该在作为输入提供的每个样本上运行。问题在于将输入作为数组传递是最方便的,该数组的最后一个轴用作“样本轴”(每个索引都是完整样本的轴)
我有一个可行的解决方案,基本上只是在 listcomp 中执行此操作,然后将其转换回 numpy 数组以进行其余计算。 (如果您想查看定义的任何函数,请告诉我,但我认为它们的相关性不大)
class GeometricNeuralNet(object):
def __init__(self, c, weight_domain=math.log(2)):
"""
Dimensions of c should be a tuple that indicates the size of each layer.
First number should be the number of input units, and the last should be the number of output units.
Other entries should be the sizes of hidden layers.
"""
weight_matrix = lambda a, b: np.exp(np.random.uniform(-weight_domain, weight_domain, [a,b]))
self.weights = [weight_matrix(c[i], c[i+1]) for i in range(len(c) - 1)]
self.predict = lambda input_vector, end=None: reduce(transfer_function, [input_vector] + self.weights[:end])
def train(self, samples, outputs, learning_rate):
# Forward Pass
true_inputs = np.array([self.predict(sample, -1) for sample in samples])
print true_inputs.shape
我对这段代码的主要问题是true_inputs 的计算方式很奇怪。有没有办法解决? np.vectorize 和 np.frompyfunc 似乎不允许轴参数,这在这里真的很重要。
编辑:
这是transfer_function 方法。
def transfer_function(x, y):
return gmean(np.power(x, y.T), axis=1)
【问题讨论】:
-
我认为我们需要查看
transfer_function才能帮助您。还有一个原因是您使用 lambdas 而不是在类中定义正确的方法? -
@BiRico 已发布,尽管我仍然觉得它不相关。大多数情况下只是为了代码整洁而使用 lambda,而不是反对编写完整的方法,只是看起来它们真的不值得一个完整的方法。
标签: python arrays numpy vectorization