如何将此源更改为逻辑回归?[关闭]

how can I change this source to logistic regression? [closed]

提问人:YOUNGKYUN OH 提问时间:11/17/2023 最后编辑:YOUNGKYUN OH 更新时间:11/19/2023 访问量:28

问:


想改进这个问题吗?更新问题,使其仅通过编辑这篇文章来关注一个问题。

4天前关闭。

我想将此代码更改为逻辑回归!!但我不知道该怎么做:(请帮帮我!!

import tensorflow as tf
import numpy as np
import time

x = tf.constant([3., 4., 5., 6., 7., 8.])
y = tf.constant([1., 1., 1., 0., 0., 0.])
w = tf.Variable(1.)
b = tf.Variable(0.5)
learning_rate = 0.01
epochs = 1000

def train_step(x, y):
    with tf.GradientTape() as t:
        y_hat = w * x + b
        loss = (y_hat - y) ** 2
    grads = t.gradient(loss, [w, b])
    w.assign_sub(learning_rate * grads[0])
    b.assign_sub(learning_rate * grads[1])

start_t = time.time()
train_step_graph = tf.function(train_step)
for i in range(epochs):
    for k in range(len(y)):
        train_step_graph(x[k], y[k])
print(time.time() - start_t)

print('w: {:8.5f}    b: {:8.5f}'.format(w.numpy(), b.numpy()))

f = 'x:{:8.5f} --> y:{:8.5f}'
for k in range(len(y)):
    y_hat = w * x[k] + b
    print(f.format(x[k].numpy(), y_hat.numpy()))

我听说它可能很简单,只是尝试将其更改为使用 sigmoid 函数?y_hat = w * x + b

但我完全不知道:(太可悲了。

Python 深度学习 逻辑回归

评论


答: 暂无答案