“RefVariable”对象没有属性“_id”

2023-11-27

我正在尝试训练线性回归模型来预测金县的房价。我是按照教程一步步操作的。然而,当我最小化损失函数时,我收到错误:

'RefVariable' object has no attribute '_id'

我正在遵循一个简单的教程来学习如何训练简单的线性回归模型。我还没有真正找到有关此类错误的任何信息。请注意,我在这个项目中使用 Google Colab。这是整个错误:

'RefVariable' object has no attribute '_id'

The above exception was the direct cause of the following exception:

SystemError                               Traceback (most recent call last)
<ipython-input-31-17eaadb45902> in <module>()
     15   #minimize the loss function
     16 
---> 17   opt.minimize(lambda: loss_function(intercept,slope,price_batch,size_batch),var_list=[intercept,slope])
     18 
     19 

3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/tape.py in watch(tape, tensor)
     57 def watch(tape, tensor):
     58   """Marks this tensor to be watched by the given tape."""
---> 59   pywrap_tensorflow.TFE_Py_TapeWatch(tape._tape, tensor)  # pylint: disable=protected-access
     60 
     61 

SystemError: <built-in function TFE_Py_TapeWatch> returned a result with an error set

这是我到目前为止所写的:

import tensorflow as tf
import numpy as np
import pandas as pd

#define trainable variables 
#for linear regression this is the intercept and the slope

intercept = tf.Variable(0.1, tf.float32)
slope = tf.Variable(0.1, tf.float32)

#define a linear regression function
def linear_regression(intercept,slope, features):  
  return intercept + slope*features

#compute predicted values and return loss function
def loss_function (intercept,slope,targets,features):
  predictions = linear_regression(intercept,slope,features)
  return tf.keras.losses.mse(targets,predictions)

#OPTIMIZER
opt = tf.keras.optimizers.Adam()

for batch in pd.read_csv('kc_house_data.csv', chunksize = 100):
  #extract the target and feature columns  
  price_batch = np.array(batch['price'], np.float32) 
  size_batch = np.array(batch['sqft_lot'], np.float32)

  #minimize the loss function 
  opt.minimize(lambda: loss_function(intercept,slope,price_batch,size_batch),var_list=[intercept,slope])

print(intercept.numpy(), slope.numpy())

您忘记启用急切执行模式。

在导入语句后添加以下行:

tf.enable_eager_execution()

更新的代码:

import tensorflow as tf
import numpy as np
import pandas as pd

tf.enable_eager_execution()

#define trainable variables 
#for linear regression this is the intercept and the slope
intercept = tf.Variable(0.1, tf.float32)
slope = tf.Variable(0.1, tf.float32)

#define a linear regression function
def linear_regression(intercept,slope, features):
  return intercept + slope*features

#compute predicted values and return loss function
def loss_function (intercept,slope,targets,features):
  predictions = linear_regression(intercept,slope,features)
  return tf.keras.losses.mse(targets,predictions)

#OPTIMIZER
opt = tf.train.AdamOptimizer()

for batch in pd.read_csv('kc_house_data.csv', chunksize = 100):
  #extract the target and feature columns
  price_batch = np.array(batch['price'], np.float32)
  size_batch = np.array(batch['sqft_lot'], np.float32)

  loss_function(intercept,slope,price_batch,size_batch)

  #minimize the loss function
  opt.minimize(lambda: loss_function(intercept,slope,price_batch,size_batch), var_list=[intercept,slope])

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

“RefVariable”对象没有属性“_id” 的相关文章

  • Python PAM 模块的安全问题?

    我有兴趣编写一个 PAM 模块 该模块将利用流行的 Unix 登录身份验证机制 我过去的大部分编程经验都是使用 Python 进行的 并且我正在交互的系统已经有一个 Python API 我用谷歌搜索发现pam python http pa
  • 如何使用固定的 pandas 数据框进行动态 matplotlib 绘图?

    我有一个名为的数据框benchmark returns and strategy returns 两者具有相同的时间跨度 我想找到一种方法以漂亮的动画风格绘制数据点 以便它显示逐渐加载的所有点 我知道有一个matplotlib animat
  • 如何生成给定范围内的回文数列表?

    假设范围是 1 X 120 这是我尝试过的 gt gt gt def isPalindrome s check if a number is a Palindrome s str s return s s 1 gt gt gt def ge
  • 如何在android上的python kivy中关闭应用程序后使服务继续工作

    我希望我的服务在关闭应用程序后继续工作 但我做不到 我听说我应该使用startForeground 但如何在Python中做到这一点呢 应用程序代码 from kivy app import App from kivy uix floatl
  • 如何在 Sublime Text 2 的 OSX 终端中显示构建结果

    我刚刚从 TextMate 切换到 Sublime Text 2 我非常喜欢它 让我困扰的一件事是默认的构建结果显示在 ST2 的底部 我的程序产生一些很长的结果 显示它的理想方式 如在 TM2 中 是并排查看它们 如何在 Mac 操作系统
  • 如何使用包含代码的“asyncio.sleep()”进行单元测试?

    我在编写 asyncio sleep 包含的单元测试时遇到问题 我要等待实际的睡眠时间吗 I used freezegun到嘲笑时间 当我尝试使用普通可调用对象运行测试时 这个库非常有用 但我找不到运行包含 asyncio sleep 的测
  • __del__ 真的是析构函数吗?

    我主要用 C 做事情 其中 析构函数方法实际上是为了销毁所获取的资源 最近我开始使用python 这真的很有趣而且很棒 我开始了解到它有像java一样的GC 因此 没有过分强调对象所有权 构造和销毁 据我所知 init 方法对我来说在 py
  • 安装后 Anaconda 提示损坏

    我刚刚安装张量流GPU创建单独的后环境按照以下指示here https github com antoniosehk keras tensorflow windows installation 但是 安装后当我关闭提示窗口并打开新航站楼弹出
  • keras加载模型错误尝试将包含17层的权重文件加载到0层的模型中

    我目前正在使用 keras 开发 vgg16 模型 我用我的一些图层微调 vgg 模型 拟合我的模型 训练 后 我保存我的模型model save name h5 可以毫无问题地保存 但是 当我尝试使用以下命令重新加载模型时load mod
  • python pandas 中的双端队列

    我正在使用Python的deque 实现一个简单的循环缓冲区 from collections import deque import numpy as np test sequence np array range 100 2 resha
  • python 集合可以包含的值的数量是否有限制?

    我正在尝试使用 python 设置作为 mysql 表中 ids 的过滤器 python集存储了所有要过滤的id 现在大约有30000个 这个数字会随着时间的推移慢慢增长 我担心python集的最大容量 它可以包含的元素数量有限制吗 您最大
  • HTTPS 代理不适用于 Python 的 requests 模块

    我对 Python 还很陌生 我一直在使用他们的 requests 模块作为 PHP 的 cURL 库的替代品 我的代码如下 import requests import json import os import urllib impor
  • 如何改变Python中特定打印字母的颜色?

    我正在尝试做一个简短的测验 并且想将错误答案显示为红色 欢迎来到我的测验 您想开始吗 是的 祝你好运 法国的首都是哪里 法国 随机答案不正确的答案 我正在尝试将其显示为红色 我的代码是 print Welcome to my Quiz be
  • Python 3 中“map”类型的对象没有 len()

    我在使用 Python 3 时遇到问题 我得到了 Python 2 7 代码 目前我正在尝试更新它 我收到错误 类型错误 map 类型的对象没有 len 在这部分 str len seed candidates 在我像这样初始化它之前 se
  • 从 pygame 获取 numpy 数组

    我想通过 python 访问我的网络摄像头 不幸的是 由于网络摄像头的原因 openCV 无法工作 Pygame camera 使用以下代码就像魅力一样 from pygame import camera display camera in
  • Nuitka 未使用 nuitka --recurse-all hello.py [错误] 编译 exe

    我正在尝试通过 nuitka 创建一个简单的 exe 这样我就可以在我的笔记本电脑上运行它 而无需安装 Python 我在 Windows 10 上并使用 Anaconda Python 3 我输入 nuitka recurse all h
  • VSCode:调试配置中的 Python 路径无效

    对 Python 和 VSCode 以及 stackoverflow 非常陌生 直到最近 我已经使用了大约 3 个月 一切都很好 当尝试在调试器中运行任何基本的 Python 程序时 弹出窗口The Python path in your
  • 用于运行可执行文件的python多线程进程

    我正在尝试将一个在 Windows 上运行可执行文件并管理文本输出文件的 python 脚本升级到使用多线程进程的版本 以便我可以利用多个核心 我有四个独立版本的可执行文件 每个线程都知道要访问它们 这部分工作正常 我遇到问题的地方是当它们
  • 协方差矩阵的对角元素不是 1 pandas/numpy

    我有以下数据框 A B 0 1 5 1 2 6 2 3 7 3 4 8 我想计算协方差 a df iloc 0 values b df iloc 1 values 使用 numpy 作为 cov numpy cov a b I get ar
  • Pandas 与 Numpy 数据帧

    看这几行代码 df2 df copy df2 1 df 1 df 1 values 1 df2 ix 0 0 我们的教练说我们需要使用 values属性来访问底层的 numpy 数组 否则我们的代码将无法工作 我知道 pandas Data

随机推荐