张量流逻辑回归矩阵

tensorflow logistic regression matrix

你好,我是 tensorflow 的新手,我正在感受它。所以我被赋予了将这 4 个矩阵相乘的任务。我能够做到这一点,但现在我被要求从 (16,8) 和 (8,4) 的乘积中获取 (16,4) 输出,并对所有输出应用物流功能。然后将这个形状为 (16,4) 的新矩阵乘以 (4,2) 矩阵。获取这些 (16,2) 输出并对它们应用 Logistics 函数。现在将这个新的 (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)

我认为你的想法是正确的。逻辑函数只是 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 之间。然后您可以继续进行分配要求的其余乘法.