小编Cal*_*xc1的帖子

我如何做一个模仿'keepdims'的einsum?

一个python问题:我有一个np.einsum操作,我正在一对3d数组上做:

return np.einsum('ijk, ijk -> ik', input_array, self._beta_array)
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是结果是2d; 该操作折叠'j'维度.我喜欢做的是让它保留'j'维度,类似于'keepdims'在np.sum函数中的工作方式.

我可以将结果包装在np.expand_dims中,但这对我来说似乎效率低下.我宁愿找到一些方法来调整einsum来输出我想要的东西.

这是真的吗?

python numpy numpy-einsum

3
推荐指数
1
解决办法
85
查看次数

Tensorflow(GPU)与Numpy

所以我有两个使用梯度下降的线性回归实现.一个在Tensorflow,一个在Numpy.我发现Numpy中的那个比Tensorflow快3倍.这是我的代码 -

Tensorflow:

class network_cluster(object):
    def __init__(self, data_frame, feature_cols, label_cols):
        self.init_data(data_frame, feature_cols, label_cols)
        self.init_tensors()

    def init_data(self, data_frame, feature_cols, label_cols):
        self.data_frame = data_frame
        self.feature_cols = feature_cols
        self.label_cols = label_cols

    def init_tensors(self):
        self.features = tf.placeholder(tf.float32)
        self.labels = tf.placeholder(tf.float32)

        self.weights = tf.Variable(tf.random_normal((len(self.feature_cols), len(self.label_cols))))
        self.const = tf.Variable(tf.random_normal((len(self.label_cols),)))

    def linear_combiner(self):
        return tf.add(tf.matmul(self.features, self.weights), self.const)

    def predict(self):
        return self.linear_combiner()

    def error(self):
        return tf.reduce_mean(tf.pow(self.labels - self.predict(), 2), axis = 0)

    def learn_model(self, epocs = 100):
        optimizer = tf.train.AdadeltaOptimizer(1).minimize(self.error())

        error_rcd = []
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            for …
Run Code Online (Sandbox Code Playgroud)

python numpy tensorflow tensorflow-gpu

2
推荐指数
1
解决办法
676
查看次数

标签 统计

numpy ×2

python ×2

numpy-einsum ×1

tensorflow ×1

tensorflow-gpu ×1