张量流逻辑回归矩阵

问题描述 投票:1回答:1

你好,我是张力流的新手,我对此感觉很好。所以我被赋予了将这4个矩阵相乘的任务。我能够做到这一点,但现在我被要求从(16,8)和(8,4)的乘法中取(16,4)输出并在所有输出上应用后勤功能。然后将这个新的形状矩阵(16,4)乘以(4,2)矩阵。获取这些(16,2)输出并对它们应用后勤功能。现在将这个新的(16,2)矩阵乘以(2,1)矩阵。我想通过矩阵操作可以完成所有这些。我对如何解决它感到困惑,因为我只是理解线性回归。我知道他们很相似,但我不知道如何应用它。请给我任何提示。不,我不是要求某人完成我只是想要一个比我给出的更好的例子,因为我无法弄清楚如何使用矩阵来实现逻辑功能。这就是我到目前为止所拥有的

import tensorflow as ts
import numpy as np
import os
# AWESOME SAUCE WARNING MESSAGE WAS GETTING ANNOYING
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' #to avoid warnings about compilation

# for different matrix asked to multiply with
# use random for random numbers in each matrix
m1 = np.random.rand(16,8)
m2 = np.random.rand(8,4)
m3 = np.random.rand(4,2)
m4 = np.random.rand(2,1)


# using matmul to mulitply could use @ or dot() but using tensorflow
c = ts.matmul(m1,m2)
d = ts.matmul(c,m3)
e = ts.matmul(d, m4)

#attempting to create log regression
arf = ts.Variable(m1,name = "ARF")



with ts.Session() as s:
    r1 = s.run(c)
    print("M1 * M2: \n",r1)

    r2 = s.run(d)
    print("Result of C * M3: \n ", r2)

    r3 = s.run(e)
    print("Result of D * M4: \n",r3)

    #learned i cant reshape just that easily
    #r4 = ts.reshape(m1,(16,4))
    #print("Result of New M1: \n", r4)
tensorflow ipython linear-regression logistic-regression
1个回答
0
投票

我认为你有正确的想法。逻辑函数只是1 / (1 + exp(-z)),其中z是你想要应用它的矩阵。考虑到这一点,你可以简单地做:

logistic = 1 / (1 + ts.exp(-c))

这将按元素方式应用于输入。结果就是:

    lg = s.run(logistic)
    print("Result of logistic function \n ", lg)

...将打印一个与c(16,4)相同大小的矩阵,其中所有值都在0和1之间。然后,您可以继续执行分配要求的其余乘法。

© www.soinside.com 2019 - 2024. All rights reserved.