当前位置: 首页 > news >正文

矩阵补全IGMC 学习笔记

目录

Inductive Graph-based Matrix Completion (IGMC) 模型

igmc推理示例:


Inductive Graph-based Matrix Completion (IGMC) 模型

原版代码:

IGMC/models.py at master · muhanzhang/IGMC · GitHub

GNN推理示例

torch_geometric版本:torch_geometric-2.5.3

原版报错,edge_type找不到,通过删除参数修正的:

import torch
import torch.nn.functional as F
from torch.nn import Linear
from torch_geometric.nn import GCNConv, global_add_pool
from torch_geometric.utils import dropout_adj
from torch_geometric.data import Data, DataLoader
class GNN(torch.nn.Module):# a base GNN class, GCN message passing + sum_poolingdef __init__(self, dataset, gconv=GCNConv, latent_dim=[32, 32, 32, 1],regression=False, adj_dropout=0.2, force_undirected=False):super(GNN, self).__init__()self.regression = regressionself.adj_dropout = adj_dropoutself.force_undirected = force_undirectedself.convs = torch.nn.ModuleList()self.convs.append(gconv(dataset.num_features, latent_dim[0]))for i in range(0, len(latent_dim)-1):self.convs.append(gconv(latent_dim[i], latent_dim[i+1]))self.lin1 = Linear(sum(latent_dim), 128)if self.regression:self.lin2 = Linear(128, 1)else:self.lin2 = Linear(128, dataset.num_classes)def reset_parameters(self):for conv in self.convs:conv.reset_parameters()self.lin1.reset_parameters()self.lin2.reset_parameters()def forward(self, data):x, edge_index, batch = data.x, data.edge_index, data.batchif self.adj_dropout > 0:edge_index, _ = dropout_adj(edge_index, p=self.adj_dropout,force_undirected=self.force_undirected, num_nodes=len(x),training=self.training)concat_states = []for conv in self.convs:x = torch.tanh(conv(x, edge_index))concat_states.append(x)concat_states = torch.cat(concat_states, 1)x = global_add_pool(concat_states, batch)x = F.relu(self.lin1(x))x = F.dropout(x, p=0.5, training=self.training)x = self.lin2(x)if self.regression:return x[:, 0]else:return F.log_softmax(x, dim=-1)def __repr__(self):return self.__class__.__name__# 创建一个简单的数据类,用于模拟数据集属性
class SimpleDataset:num_features = 2num_classes = 2# 创建一个简单的图数据集
edge_index = torch.tensor([[0, 1, 2, 3], [1, 0, 3, 2]], dtype=torch.long)
x = torch.tensor([[1, 0], [0, 1], [1, 0], [0, 1]], dtype=torch.float)
batch = torch.tensor([0, 0, 1, 1], dtype=torch.long)# 使用 Data 类构建图数据
data = Data(x=x, edge_index=edge_index, batch=batch)# 构建 DataLoader
loader = DataLoader([data], batch_size=2, shuffle=False)dataset = SimpleDataset()# 实例化模型
model = GNN(dataset)# 模型推理
model.eval()
for data in loader:out = model(data)print(out)

igmc推理示例:


import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Linear, Conv1d
from torch_geometric.nn import GCNConv, RGCNConv, global_sort_pool, global_add_pool
from torch_geometric.utils import dropout_adj
from util_functions import *
import pdb
import time
from torch_geometric.data import Data, DataLoader
class GNN(torch.nn.Module):# a base GNN class, GCN message passing + sum_poolingdef __init__(self, dataset, gconv=GCNConv, latent_dim=[32, 32, 32, 1],regression=False, adj_dropout=0.2, force_undirected=False):super(GNN, self).__init__()self.regression = regressionself.adj_dropout = adj_dropoutself.force_undirected = force_undirectedself.convs = torch.nn.ModuleList()self.convs.append(gconv(dataset.num_features, latent_dim[0]))for i in range(0, len(latent_dim)-1):self.convs.append(gconv(latent_dim[i], latent_dim[i+1]))self.lin1 = Linear(sum(latent_dim), 128)if self.regression:self.lin2 = Linear(128, 1)else:self.lin2 = Linear(128, dataset.num_classes)def reset_parameters(self):for conv in self.convs:conv.reset_parameters()self.lin1.reset_parameters()self.lin2.reset_parameters()def forward(self, data):x, edge_index, batch = data.x, data.edge_index, data.batchif self.adj_dropout > 0:# edge_index, edge_type = dropout_adj(#     edge_index, edge_type, p=self.adj_dropout,#     force_undirected=self.force_undirected, num_nodes=len(x),#     training=self.training# )edge_index, edge_type = dropout_adj(edge_index, p=self.adj_dropout, force_undirected=self.force_undirected, num_nodes=len(x), training=self.training)concat_states = []for conv in self.convs:x = torch.tanh(conv(x, edge_index))concat_states.append(x)concat_states = torch.cat(concat_states, 1)x = global_add_pool(concat_states, batch)x = F.relu(self.lin1(x))x = F.dropout(x, p=0.5, training=self.training)x = self.lin2(x)if self.regression:return x[:, 0]else:return F.log_softmax(x, dim=-1)def __repr__(self):return self.__class__.__name__
class IGMC(GNN):# The GNN model of Inductive Graph-based Matrix Completion.# Use RGCN convolution + center-nodes readout.def __init__(self, dataset, gconv=RGCNConv, latent_dim=[32, 32, 32, 32],num_relations=5, num_bases=2, regression=False, adj_dropout=0.2,force_undirected=False, side_features=False, n_side_features=0,multiply_by=1):super(IGMC, self).__init__(dataset, GCNConv, latent_dim, regression, adj_dropout, force_undirected)self.multiply_by = multiply_byself.convs = torch.nn.ModuleList()self.convs.append(gconv(dataset.num_features, latent_dim[0], num_relations, num_bases))for i in range(0, len(latent_dim)-1):self.convs.append(gconv(latent_dim[i], latent_dim[i+1], num_relations, num_bases))self.lin1 = Linear(2*sum(latent_dim), 128)self.side_features = side_featuresif side_features:self.lin1 = Linear(2*sum(latent_dim)+n_side_features, 128)def forward(self, data):start = time.time()x, edge_index, edge_type, batch = data.x, data.edge_index, data.edge_type, data.batchif self.adj_dropout > 0:edge_index, edge_type = dropout_adj(edge_index, edge_type, p=self.adj_dropout,force_undirected=self.force_undirected, num_nodes=len(x),training=self.training)concat_states = []for conv in self.convs:x = torch.tanh(conv(x, edge_index, edge_type))concat_states.append(x)concat_states = torch.cat(concat_states, 1)users = data.x[:, 0] == 1items = data.x[:, 1] == 1x = torch.cat([concat_states[users], concat_states[items]], 1)if self.side_features:x = torch.cat([x, data.u_feature, data.v_feature], 1)x = F.relu(self.lin1(x))x = F.dropout(x, p=0.5, training=self.training)x = self.lin2(x)if self.regression:return x[:, 0] * self.multiply_byelse:return F.log_softmax(x, dim=-1)class SimpleDataset:num_features = 2num_classes = 2# 创建一个简单的图数据集
edge_index = torch.tensor([[0, 1, 2, 3], [1, 0, 3, 2]], dtype=torch.long)
edge_type = torch.tensor([0, 1, 2, 3], dtype=torch.long)
x = torch.tensor([[1, 0], [0, 1], [1, 0], [0, 1]], dtype=torch.float)
batch = torch.tensor([0, 0, 1, 1], dtype=torch.long)# 使用 Data 类构建图数据
data = Data(x=x, edge_index=edge_index,edge_type=edge_type, batch=batch)# 构建 DataLoader
loader = DataLoader([data], batch_size=2, shuffle=False)dataset = SimpleDataset()# 实例化模型
model = IGMC(dataset)# 模型推理
model.eval()
for data in loader:out = model(data)print(out)

相关文章:

矩阵补全IGMC 学习笔记

目录 Inductive Graph-based Matrix Completion (IGMC) 模型 igmc推理示例: Inductive Graph-based Matrix Completion (IGMC) 模型 原版代码: IGMC/models.py at master muhanzhang/IGMC GitHub GNN推理示例 torch_geometric版本:tor…...

面试题之CSS

1. 引入样式的方式 外部样式 link import 区别 内部样式 /* 写在头部标签 */ <style></style>行内样式 2. 三行代码画三角形 .triangle{width:0px;border:100px solid;border-color:transparent transparent transparent red; }3.属性的继承 可继承的属性 …...

MFC扩展库BCGControlBar Pro v35.0新版亮点:重新设计的工具栏编辑器等

BCGControlBar库拥有500多个经过全面设计、测试和充分记录的MFC扩展类。 我们的组件可以轻松地集成到您的应用程序中&#xff0c;并为您节省数百个开发和调试时间。 BCGControlBar专业版 v35.0已全新发布了&#xff0c;这个版本改进类Visual Studio 2022的视觉主题、增强对多个…...

python调用SDK的问题

问题&#xff1a;Could not find module MvCameraControl.dll 原因&#xff1a;识别环境变量runtime异常 解决&#xff1a;指定具体绝对地址即可。MvCameraControl.dll的位置C:\Program Files (x86)\Common Files\MVS\Runtime\Win64_x64 MvCamCtrldll WinDLL("MvCamer…...

html入门综合练习

综合练习 通过实际项目练习可以更好地理解和掌握HTML、CSS和JavaScript。以下是几个综合练习项目的建议&#xff1a; 项目1&#xff1a;个人简历网页 创建一个包含以下内容的个人简历网页&#xff1a; 个人简介&#xff08;姓名、照片、联系方式&#xff09;教育背景工作经…...

函数模板的具体化

函数模板优点是通用性&#xff0c;可以解决某个方面的普遍性问题&#xff0c;但是这个世界上的事情不是绝对的&#xff0c;有普遍的&#xff0c;就有绝对的。举个栗子&#xff1a; #include <iostream> using namespace std; template <typename T> void Swap(T &…...

【Linux 内存管理】

文章目录 1. 为什么要有虚拟内存呢&#xff1f;&#x1f50d; 1. 为什么要有虚拟内存呢&#xff1f;&#x1f50d;...

AJAX 数据库

AJAX 数据库 1. 引言 AJAX(Asynchronous JavaScript and XML)是一种流行的网络开发技术,它允许网页在不重新加载整个页面的情况下与服务器交换数据和更新部分网页内容。AJAX技术与数据库的结合,为现代网络应用提供了更加丰富和动态的用户体验。本文将探讨AJAX如何与数据库…...

力扣719.找出第K小的数对距离

力扣719.找出第K小的数对距离 二分答案 朴素版 双指针遍历数组 超过界限break auto check [&](int mid) -> bool{int res0;for(int i0;i<n-1;i)for(int ji1;j<n;j){if(nums[j] - nums[i] > mid) break;elseif(res > k) return true;}return false;};优…...

富格林:掌握可信出金交易策略

富格林认为&#xff0c;黄金市场是起起落落&#xff0c;似乎机遇无处不在&#xff0c;但很少有人能真正抓住机遇。黄金可以做多也可以做空&#xff0c;做空主要是为了从黄金价格波动的价差中获利。只有采用正规可信的操作技巧&#xff0c;才能实现顺利获利出金&#xff0c;减少…...

HCS-华为云Stack-容器网络

HCS-华为云Stack-容器网络 容器隧道overlay VPC网络...

【CSS in Depth2精译】1.1 层叠

CSS 本质上就是声明规则&#xff0c;并让这些特定的规则在各种情况下生效。一个类添加到某个元素上&#xff0c;则应用这个类包含的这一些样式&#xff1b;元素 X 是元素 Y 的一个子节点&#xff0c;则应用另一些样式。浏览器于是根据这些规则&#xff0c;判定所有样式生效的具…...

【读博日记】拓扑结构(待修正)

Topology 拓扑学 内容来源于互联网&#xff0c;还在甄别中——20240617 拓扑结构指把实体抽象成与其形状大小无关的点&#xff0c;把连接实体的线路抽象成线&#xff0c;再研究这些电线之间的关系。 所谓相似的拓扑结构&#xff1a; 例如一个圆环变成正方形、长方形、三角形…...

QT 中setVisible()和setEnabled()的区别

setVisible(bool)和setEnabled(true)在PyQt&#xff08;以及其他类似的图形用户界面框架&#xff09;中分别用于控制控件的可见性和可用性&#xff0c;它们之间的主要区别如下&#xff1a; setVisible(bool) 功能&#xff1a;这个函数用于设置QWidget控件的可见状态。参数&am…...

速度(velocity)、加速度(acceleration)、急动度(jerk)和弹跳度(snap)傻傻分不清楚?

...

【YashanDB知识库】PHP使用ODBC使用数据库绑定参数功能异常

【问题分类】驱动使用 【关键字】ODBC、驱动使用、PHP 【问题描述】 PHP使用PDO_ODBC连接yashan数据库&#xff0c;使用绑定参数获取数据时&#xff0c;客户现场出现报错 本地复现未出现异常报错&#xff0c;但是无法正确获取数据。 【问题原因分析】开启ODBC报错日志后&am…...

初级篇-Docker容器知识

Docker容器 容器主要是解决跨平台、跨服务运行环境的问题 容器将运行业务应用所需要的东西进行打包&#xff0c;包括依赖项、配置、脚本、二进制文件等。在容器中运行镜像&#xff0c;不用担心不同环境下运行不一致的问题。 容器本质上是一个特殊的进程&#xff0c;将资源、…...

【抽代复习笔记】19-群(十三):奇偶置换、循环置换的几个定理及例题

定义&#xff1a; ①在Sn中&#xff0c;能够表示为奇数多个对换乘积的置换称为“奇置换”&#xff0c;能够表示为偶数多个对换乘积的置换称为“偶置换”&#xff1b; ②所有偶置换的集合记为An。 例1&#xff1a;&#xff08;1&#xff09;计算S1和S2中奇、偶置换的数目&…...

RT-Thread简介及启动流程分析

阅读引言&#xff1a; 最近在学习RT-Thread的内部机制&#xff0c;觉得这个启动流程和一些底层原理还是挺重要的&#xff0c; 所以写下此文。 目录 1&#xff0c; RT-Thread简介 2&#xff0c;RT-Thread任务的几种状态 3&#xff0c; 学习资源推荐 4&#xff0c; 启动流程分…...

MCU嵌入式AI开发笔记-视频笔记同步更新

MCU嵌入式AI开发笔记 抖音B站等站点笔记视频同步更新 01嵌入式AI大的方向 STM32跑神经网络 http://news.eeworld.com.cn/mp/EEWorld/a134877.jspx 为什么可以在STM32上面跑神经网络?简而言之就是使用STM32CubeMX中的X-Cube-AI扩展包将当前比较热门的AI框架进行C代码的转化,…...

工业安全零事故的智能守护者:一体化AI智能安防平台

前言&#xff1a; 通过AI视觉技术&#xff0c;为船厂提供全面的安全监控解决方案&#xff0c;涵盖交通违规检测、起重机轨道安全、非法入侵检测、盗窃防范、安全规范执行监控等多个方面&#xff0c;能够实现对应负责人反馈机制&#xff0c;并最终实现数据的统计报表。提升船厂…...

java调用dll出现unsatisfiedLinkError以及JNA和JNI的区别

UnsatisfiedLinkError 在对接硬件设备中&#xff0c;我们会遇到使用 java 调用 dll文件 的情况&#xff0c;此时大概率出现UnsatisfiedLinkError链接错误&#xff0c;原因可能有如下几种 类名错误包名错误方法名参数错误使用 JNI 协议调用&#xff0c;结果 dll 未实现 JNI 协…...

[10-3]软件I2C读写MPU6050 江协科技学习笔记(16个知识点)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16...

OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别

OpenPrompt 和直接对提示词的嵌入向量进行训练有什么区别 直接训练提示词嵌入向量的核心区别 您提到的代码: prompt_embedding = initial_embedding.clone().requires_grad_(True) optimizer = torch.optim.Adam([prompt_embedding...

Fabric V2.5 通用溯源系统——增加图片上传与下载功能

fabric-trace项目在发布一年后,部署量已突破1000次,为支持更多场景,现新增支持图片信息上链,本文对图片上传、下载功能代码进行梳理,包含智能合约、后端、前端部分。 一、智能合约修改 为了增加图片信息上链溯源,需要对底层数据结构进行修改,在此对智能合约中的农产品数…...

人工智能(大型语言模型 LLMs)对不同学科的影响以及由此产生的新学习方式

今天是关于AI如何在教学中增强学生的学习体验&#xff0c;我把重要信息标红了。人文学科的价值被低估了 ⬇️ 转型与必要性 人工智能正在深刻地改变教育&#xff0c;这并非炒作&#xff0c;而是已经发生的巨大变革。教育机构和教育者不能忽视它&#xff0c;试图简单地禁止学生使…...

【JVM面试篇】高频八股汇总——类加载和类加载器

目录 1. 讲一下类加载过程&#xff1f; 2. Java创建对象的过程&#xff1f; 3. 对象的生命周期&#xff1f; 4. 类加载器有哪些&#xff1f; 5. 双亲委派模型的作用&#xff08;好处&#xff09;&#xff1f; 6. 讲一下类的加载和双亲委派原则&#xff1f; 7. 双亲委派模…...

莫兰迪高级灰总结计划简约商务通用PPT模版

莫兰迪高级灰总结计划简约商务通用PPT模版&#xff0c;莫兰迪调色板清新简约工作汇报PPT模版&#xff0c;莫兰迪时尚风极简设计PPT模版&#xff0c;大学生毕业论文答辩PPT模版&#xff0c;莫兰迪配色总结计划简约商务通用PPT模版&#xff0c;莫兰迪商务汇报PPT模版&#xff0c;…...

关于uniapp展示PDF的解决方案

在 UniApp 的 H5 环境中使用 pdf-vue3 组件可以实现完整的 PDF 预览功能。以下是详细实现步骤和注意事项&#xff1a; 一、安装依赖 安装 pdf-vue3 和 PDF.js 核心库&#xff1a; npm install pdf-vue3 pdfjs-dist二、基本使用示例 <template><view class"con…...

掌握 HTTP 请求:理解 cURL GET 语法

cURL 是一个强大的命令行工具&#xff0c;用于发送 HTTP 请求和与 Web 服务器交互。在 Web 开发和测试中&#xff0c;cURL 经常用于发送 GET 请求来获取服务器资源。本文将详细介绍 cURL GET 请求的语法和使用方法。 一、cURL 基本概念 cURL 是 "Client URL" 的缩写…...