【问题标题】:How to create this custom ANN using tensorflow?如何使用 tensorflow 创建这个自定义 ANN?
【发布时间】:2021-11-30 06:30:06
【问题描述】:

我正在尝试使用 tensorflow 创建此自定义 ANN。这是玩具网络和代码的图像。

import tensorflow as tf
import numpy as np

in = np.array([1, 2, 3, 4], , dtype="float32")
y_true = np.array([10, 11], , dtype="float32")


# w is vector of weights
# y_pred = np.array([in[0]*w[0]+in[1]*w[0]], [in[2]*w[1]+in[3]*w[1]] )
# y_pred1 = 1 / (1 + tf.math.exp(-y_pred)) # sigmoid activation function

def loss_fun(y_true, y_pred1):
    loss1 = tf.reduce_sum(tf.pow(y_pred1 - y_true, 2))   

# model.compile(loss=loss_fun,  optimizer='adam', metrics=['accuracy'])

这个网络的输出到右边的另一个 ANN,我知道这些东西,但不知道如何创建连接、更新wy_pred,并编译模型。有什么帮助吗?

【问题讨论】:

    标签: tensorflow machine-learning neural-network


    【解决方案1】:

    这样的东西应该可以工作

    import tensorflow as tf
    import numpy as np
    
    def y_pred(x, w):
        return [x[0]*w[0]+x[1]*w[0], x[2]*w[1]+x[3]*w[1]]
    
    def loss_fun(y_true, y_pred):
        return tf.reduce_sum(tf.pow(y_pred - y_true, 2))
            
    x = np.array([1, 2, 3, 4], dtype="float32")
    y_true = np.array([10, 11], dtype="float32")
    w = tf.Variable(initial_value=np.random.normal(size=(2)), name='weights', dtype=tf.float32)
    xt = tf.convert_to_tensor(x)
    yt = tf.convert_to_tensor(y_true)
    sgd_opt = tf.optimizers.SGD()
    training_steps = 100
    display_steps = 10
    for step in range(training_steps):
        with tf.GradientTape() as tape:
            tape.watch(w)
            yp = y_pred(xt, w)
            loss = loss_fun(yt, yp)
        dl_dw = tape.gradient(loss, w)
        sgd_opt.apply_gradients(zip([dl_dw], [w]))
        if step % display_steps == 0:
            print(loss, w)
    

    【讨论】:

    • 谢谢。是否需要编写单独的激活函数或者我可以使用内置函数?
    • @ewr3243 您可以使用内置激活,只需更改 y_pred 函数(或通过激活运行“yp”)。您可能也希望将损失更改为交叉熵,并且 y_true 看起来也会有所不同。如果你开始改变东西,你需要小心 GradientTape,因为它需要所有东西都是张量(而不是例如 np.ndarrays)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2023-03-09
    • 2022-12-10
    • 1970-01-01
    相关资源
    最近更新 更多