张量流多元线性回归不收敛

2024-05-03

我正在尝试使用张量流训练具有正则化的多元线性回归模型。由于某种原因,我无法获取以下代码的训练部分来计算我想要用于梯度下降更新的误差。我在设置图表时做错了什么吗?

def normalize_data(matrix):
    averages = np.average(matrix,0)
    mins = np.min(matrix,0)
    maxes = np.max(matrix,0)
    ranges = maxes - mins
    return ((matrix - averages)/ranges)

def run_regression(X, Y, X_test, Y_test, lambda_value = 0.1, normalize=False, batch_size=10):
    x_train = normalize_data(X) if normalize else X
    y_train = Y
    x_test = X_test
    y_test = Y_test
    session = tf.Session()

    # Calculate number of features for X and Y
    x_features_length = len(X[0])
    y_features_length = len(Y[0])

    # Build Tensorflow graph parts
    x = tf.placeholder('float', [None, x_features_length], name="X")
    y = tf.placeholder('float', [None, y_features_length], name="Y")
    theta = tf.Variable(tf.random_normal([x_features_length, y_features_length], stddev=0.01), name="Theta")
    lambda_val = tf.constant(lambda_value)

    # Trying to implement this way http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex5/ex5.html
    y_predicted = tf.matmul(x, theta, name="y_predicted")
    regularization_cost_part = tf.cast(tf.mul(lambda_val,tf.reduce_sum(tf.pow(theta,2)), name="regularization_param"), 'float')
    polynomial_cost_part = tf.reduce_sum(tf.pow(tf.sub(y_predicted, y), 2), name="polynomial_sum")

    # Set up some summary info to debug
    with tf.name_scope('cost') as scope:
        cost_func = tf.mul(tf.cast(1/(2*batch_size), 'float'), tf.cast(tf.add(polynomial_cost_part, regularization_cost_part), 'float'))
        cost_summary = tf.scalar_summary("cost", cost_func)

    training_func = tf.train.GradientDescentOptimizer(0.03).minimize(cost_func)

    with tf.name_scope("test") as scope:
        correct_prediction = tf.sub(tf.cast(1, 'float'), tf.reduce_mean(tf.sub(y_predicted, y)))
        accuracy = tf.cast(correct_prediction, "float")
        accuracy_summary = tf.scalar_summary("accuracy", accuracy)

    saver = tf.train.Saver()
    merged = tf.merge_all_summaries()
    writer = tf.train.SummaryWriter("/tmp/football_logs", session.graph_def)
    init = tf.initialize_all_variables()

    session.run(init)
    for i in range(0, (len(x_train)/batch_size)):
        session.run(training_func, feed_dict={x: x_train[i*batch_size:i*batch_size+batch_size], y: y_train[i*batch_size:i*batch_size+batch_size]})
        if i % batch_size == 0:
            result = session.run([merged, accuracy], feed_dict={x: x_test, y: y_test})
            writer.add_summary(result[0], i)
            print "step %d, training accuracy %g"%(i, result[1])

    print "test accuracy %g"%session.run(accuracy, feed_dict={x: x_test, y: y_test})

    save_path = saver.save(session, "/tmp/football.ckpt")
    print "Model saved in file: ", save_path
    session.close()

我的输出看起来像这样

step 0, training accuracy 39.1802
step 10, training accuracy 39.1802
step 20, training accuracy 39.1802
...
step 210, training accuracy 39.1802
test accuracy 39.1802
Model saved in file:  /tmp/football.ckpt

看起来确实是学习率的问题:0.03可能太高,具体取决于您的数据的外观。另外,您可能希望以更明确的方式创建与会话分离的图表,或者甚至使用正规方程如果您的数据集具有中/低维度,则无需迭代即可达到最佳解决方案。Here https://stackoverflow.com/a/43170585/4511978我发布了一些示例,希望对您有所帮助!另外,TF教程 https://www.tensorflow.org/get_started/get_started很好地覆盖它(在该页面中搜索“完整程序”)。

但是关于您的代码,这是一个对我有用的版本:我更改了一些已弃用的函数,并且基本上将学习率设置为低得多的值alpha=1e-8,(在代码中生成的合成数据集上)似乎收敛:

test accuracy 2176.11
test accuracy 1898.6
test accuracy 1663.69
test accuracy 1458.53
test accuracy 1287.57
test accuracy 1116.9
test accuracy 969.474
test accuracy 841.028
test accuracy 738.592
test accuracy 649.891
test accuracy 565.188
test accuracy 495.33
test accuracy 438.351
test accuracy 381.161
test accuracy 333.213
test accuracy 289.575
test accuracy 254.394
test accuracy 222.836
test accuracy 197.36
test accuracy 172.788
test accuracy 152.251
test accuracy 132.664
test accuracy 115.982
test accuracy 101.021
final test accuracy 90.2555

CODE:

import tensorflow as tf
import numpy as np


# generate some dataset
DIMENSIONS = 5
DS_SIZE = 5000
TRAIN_RATIO = 0.5 # 50% of the dataset isused for training
_train_size = int(DS_SIZE*TRAIN_RATIO)
_test_size = DS_SIZE - _train_size
f = lambda(x): sum(x) # the "true" function: f = 0 + 1*x1 + 1*x2 + 1*x3 ...
noise = lambda: np.random.normal(0,10) # some noise
# training globals
LAMBDA = 1e6 # L2 regularization factor
# generate the dataset, the labels and split into train/test
ds = [[np.random.rand()*1000 for d in range(DIMENSIONS)] for _ in range(DS_SIZE)]
ds = [([1]+x, [f(x)+noise()]) for x in ds] # add x[0]=1 dimension and labels
np.random.shuffle(ds)
train_data, train_labels = zip(*ds[0:_train_size])
test_data, test_labels = zip(*ds[_train_size:])



def normalize_data(matrix):
    averages = np.average(matrix,0)
    mins = np.min(matrix,0)
    maxes = np.max(matrix,0)
    ranges = maxes - mins
    return ((matrix - averages)/ranges)

def run_regression(X, Y, X_test, Y_test, lambda_value = 0.1, normalize=False, batch_size=10, alpha=1e-8):
    x_train = normalize_data(X) if normalize else X
    y_train = Y
    x_test = X_test
    y_test = Y_test
    session = tf.Session()
    # Calculate number of features for X and Y
    x_features_length = len(X[0])
    y_features_length = len(Y[0])
    # Build Tensorflow graph parts
    x = tf.placeholder('float', [None, x_features_length], name="X")
    y = tf.placeholder('float', [None, y_features_length], name="Y")
    theta = tf.Variable(tf.random_normal([x_features_length, y_features_length], stddev=0.01), name="Theta")
    lambda_val = tf.constant(lambda_value)
    # Trying to implement this way http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex5/ex5.html
    y_predicted = tf.matmul(x, theta, name="y_predicted")
    #regularization_cost_part = tf.cast(tf.multiply(lambda_val,tf.reduce_sum(tf.pow(theta,2)), name="regularization_param"), 'float')
    #polynomial_cost_part = tf.reduce_sum(tf.pow(tf.subtract(y_predicted, y), 2), name="polynomial_sum")
    # Set up some summary info to debug
    with tf.name_scope('cost') as scope:
        #cost_func = tf.multiply(tf.cast(1/(2*batch_size), 'float'), tf.cast(tf.add(polynomial_cost_part, regularization_cost_part), 'float'))
        cost_func = (tf.nn.l2_loss(y_predicted - y)+lambda_val*tf.nn.l2_loss(theta))/float(batch_size)
        #DEPRECATED*** cost_summary = tf.scalar_summary("cost", cost_func)
        cost_summary = tf.summary.scalar('cost', cost_func)# Add a scalar summary for the snapshot loss.
    training_func = tf.train.GradientDescentOptimizer(alpha).minimize(cost_func)
    with tf.name_scope("test") as scope:
        correct_prediction = tf.subtract(tf.cast(1, 'float'), tf.reduce_mean(tf.subtract(y_predicted, y)))
        accuracy = tf.cast(correct_prediction, "float")
        #DEPRECATED*** accuracy_summary = tf.scalar_summary("accuracy", accuracy)
        #accuracy_summary = tf.summary.scalar("accuracy", accuracy)
    saver = tf.train.Saver()
    #DEPRECATED*** merged = tf.merge_all_summaries()
    merged = tf.summary.merge_all()
    #DEPRECATED*** writer = tf.train.SummaryWriter("/tmp/football_logs", session.graph_def)
    writer = tf.summary.FileWriter("/tmp/football_logs", session.graph)
    #DEPRECATED*** init = tf.initialize_all_variables()
    init = tf.global_variables_initializer()
    session.run(init)
    for i in range(1, (len(x_train)/batch_size)):
        session.run(training_func, feed_dict={x: x_train[i*batch_size:i*batch_size+batch_size], y: y_train[i*batch_size:i*batch_size+batch_size]})
        if i % batch_size == 0:
            print "test accuracy %g"%session.run(accuracy, feed_dict={x: x_test, y: y_test})
            #result = session.run([merged, accuracy], feed_dict={x: x_test, y: y_test})
            # writer.add_summary(result[0], i)
            # print "step %d, training accuracy %g"%(i, result[1])
            #writer.flush()
    print "final test accuracy %g"%session.run(accuracy, feed_dict={x: x_test, y: y_test})
    # save_path = saver.save(session, "/tmp/football.ckpt")
    # print "Model saved in file: ", save_path
    session.close()

run_regression(train_data, train_labels, test_data, test_labels, normalize=False, alpha=1e-8)

正如我所说,您可能希望更改结构以有利于可读性和可扩展性,但希望这会有所帮助!

干杯, 安德烈斯

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

张量流多元线性回归不收敛 的相关文章

随机推荐

  • C# - 平移光标

    我正在 PictureBox 控件中实现大图像的平移 并且设置适当的方向平移光标没有问题 但是 我似乎找不到用于平底锅原点的图像 内部带有箭头的圆圈 我在哪里可以找到它 我觉得image您正在寻找的内容未包含在框架中 每个应用程序都使用自己
  • 无法使用 Spring.NET 将依赖项注入到 Azure WorkerRole 对象

    我在使用 spring net 4 0 和 nhibernate 3 0 开发基于 ASP net 的 Web 应用程序方面拥有一定的经验 最近我遇到了一种情况 我需要使用 spring net 来注入我的服务依赖项 这些依赖项属于Work
  • 无法从 setup-jest.js 找到模块

    我有一个 Angular 库 正在努力将其从 Angular 11 更新到 13 Jest 在 11 和 12 中运行良好 但现在我在 v13 中遇到了很多麻烦 我已经按照此处概述的步骤 https thymikee github io j
  • createNativeQuery 设置参数

    我有以下内容 其中包含 NativeQuery 我需要在其中设置参数 但有些事情是错误的 因为未设置参数 因此查询是 SELECT movieId title genres FROM movies where title like 所以返回
  • Perl 命令或模块,如 Linux“文件”命令

    我有一个下载文件的脚本 但这些文件在下载之前没有任何有关它们的信息 在为 Linux 编写代码时 我刚刚调用了qx file filename 查看它是否是 JPEG 图像 如果不是则将其删除 然而 我现在正尝试重写为独立于平台的纯 Per
  • 如何更改 Xcode 上的导航器字体大小

    有什么办法可以改变xcode中导航器面板的字体大小吗 我已设法使用 首选项 gt 字体和颜色 更改编辑窗口中的字体大小 但这不会更改导航器的字体大小 适用于 Xcode 12 及以上版本 Its default value is match
  • 每行中最后一次出现 True 的索引

    我有一个二维数组 a False False False False False True True True True True True True True True True True True True True True True
  • 使用 Jackson 作为 Jersey 客户端序列化器

    使用 Jersey Client API 时 是否可以使用 Jackson 作为 JSON 数据的序列化器 编组器而不是 JAXB 如果可以的话该如何配置呢 好吧 我发现了 原来很简单 ClientConfig cc new Default
  • oracle 计算两个字符串中连续匹配的单词

    我想要一个返回两个字符串中单词的顺序匹配数的查询 例子 Table Id column1 column2 result 1 foo bar live foo bar 2 2 foo live tele foo tele 1 3 bar fo
  • 参数为动态的 Spark 滞后函数

    我需要在spark中实现lag函数 我可以像下面这样做 使用 hive temp Spark 表中的一些数据 假设 DF 有这些行 lagno value 0 100 0 200 2 null 3 null 其中第一列是您要使用的实际滞后数
  • onActivityresult 数据为空

    这是我的相机应用程序 我想在其中捕获图像并裁剪它 但它拍照保存在我的 myimage 目录中 但不执行裁剪功能 请我需要帮助 我是这个领域的新人 这是我的相机开源代码 Intent intent new Intent MediaStore
  • 网站性能衡量

    我需要一个免费的工具来测量网站的性能 并且不需要对代码 jsp asp 页面 进行任何更改 感谢所有帮助 对于绩效衡量 我建议您YSlow http developer yahoo com yslow 它是一个 Firefox 插件 集成了
  • ORA-01741: 非法的零长度标识符

    您好 我在 shell 脚本中使用删除查询 并且遇到了这个问题 delete from WHITELIST CLI where filecode like Line Index condense Error ERROR ORA 01741
  • 有没有办法在 .NET Core 库中包含 .NET Framework 代码?

    我们有一个商业库 我正在努力将其移植到 NET Core 我想保留其中的几个调用 以便仅在以 NET 标准运行时使用 出于好奇 一组是读取 Windows 服务器上需要凭据才能访问的文件 有没有 该调用将告诉我是否在 NET Standar
  • 虚拟键盘(类似 Swype 键盘)Windows 窗体应用程序 C#

    我正在尝试使用 c 在 Windows 窗体中创建一个类似 swype 的键盘 我有两个问题 A 我无法重现手指滑动动作 b 我无法识别不同按键下的字母 对于第一个问题 我使用了 Graphics 类和 Pen 类 并像这样使用它 bool
  • Codeigniter 中的 HTML 格式的电子邮件

    如何在 codeigniter 中发送格式化的电子邮件 我有这段代码 可以很好地发送电子邮件 但它没有按应有的方式格式化它 您可以看到显示收到电子邮件的图片 function email sender this gt load gt hel
  • 如何防止Firebase云功能“冲突”?

    我有一组具有基于时间的派生属性的对象 我有一个 Firebase 云函数 它正在侦听创建和写入以计算属性 并且运行良好 我还添加了一个通过 HTTP 触发的函数 例如 cron 在周日清晨重新计算属性 该属性每周都会更改 这工作正常 但每当
  • 是否可以在 iOS 应用程序中使用 rsync?

    是否可以在 iPhone 或 iPad 应用程序中使用 rsync lib 或者也许有任何适合通过 sftp 进行远程文件同步的替代方案 Acrosync库是一个不错的选择 我已经为它做了一个演示 它根据 RPL 许可证进行许可 并提供商业
  • jQuery Datatables 分页中如何返回特定页面?

    我在每一行都有一个数据表和 编辑 按钮 当我单击该按钮时 edit url 将打开 到目前为止一切都很好 但如果我需要返回数据表 则从第一页开始 我能为此做些什么呢 table dataTable sDom rtFip gt fnDrawC
  • 张量流多元线性回归不收敛

    我正在尝试使用张量流训练具有正则化的多元线性回归模型 由于某种原因 我无法获取以下代码的训练部分来计算我想要用于梯度下降更新的误差 我在设置图表时做错了什么吗 def normalize data matrix averages np av