r2_score_val = stats.linregress(output_val[valid_mask].tolist(), drug_graph[valid_mask].tolist()).rvalue ** 2报错NotImplementedError: Could not run 'aten::index.Tensor' with arguments from the 'SparseCPU' backend.
时间: 2024-01-31 18:01:52 浏览: 130
这个错误通常是因为 PyTorch 无法在稀疏张量上运行 `index` 操作,因为稀疏张量的索引操作会返回一个新的稀疏张量,而 PyTorch 目前无法处理这种情况。
为了解决这个问题,您可以尝试将稀疏张量转换为密集张量,然后再进行索引操作。您可以使用 `to_dense()` 方法将稀疏张量转换为密集张量,例如:
```python
output_val_dense = output_val.to_dense()
drug_graph_dense = drug_graph.to_dense()
r2_score_val = stats.linregress(output_val_dense[valid_mask].tolist(), drug_graph_dense[valid_mask].tolist()).rvalue ** 2
```
这样就可以避免在稀疏张量上进行索引操作,从而解决问题。当然,这样做可能会增加内存使用量,所以请确保您的系统有足够的内存。
相关问题
import networkx as nx import numpy as np import pandas as pd import matplotlib.pyplot as plt import networkx as nx import random df=pd.read_csv("D:\级联失效\edges.csv") G=nx.from_pandas_edgelist(df,'from','to',create_using=nx.Graph()) nx.draw(G,node_size=300,with_labels=True) As=nx.adjacency_matrix(G) A=As.todense() def f(x): F=4*x*(1-x) return F n=len(A) r=2 ohxs=0.4 step=10 d=np.zeros([n,step]) for i in range(n): d[i,0]=np.sum(A[i]) x_intial=np.zeros([n,step]) for i in range(n): x_intial[i,0]=random.random() np.set_printoptions(precision=5) h_a=100 H=np.zeros([n,step]) D=np.zeros([n,step]) for i in range(n): Deg=0 for k in range(n): if k!=i: Deg=Deg+d[k,0] D[i,0]=Deg H[i,0]=d[i,0]/D[i,0]/h_a fail_scale=np.zeros(step) fail_scale[0]=1 node_rand_id=random.randint(0,n) r=2 x_intial[node_rand_id,0]=x_intial[node_rand_id,0]+r print(x_intial) fail_node=np.zeros(n) fail_node[node_rand_id]=1 print(fail_node) np.seterr(divide='ignore',invalid='ignore') for t in range(1,step): fail_node_id=[idx for (idx,val) in enumerate(fail_node) if val ==1] for i in range(n): sum=0 for j in range(n): sum = sum+A[i,j]*f(x_intial[j,t-1])/d[i] if i in fail_node_id: x_intial[i,t-1]=0 A[i,:]=0 A[:,i]=0 else: x_intial[i,t]=H[i,t-1]*abs((1-ohxs)*f(x_intial[i,t-1])+ohxs*sum) d[i,t]=np.sum(A[i]) Deg=0 for k in range(n): if k!=i: Deg=Deg+d[i,t] D[i,t]=Deg H[i,t]=d[i,t]/D[i,t]/h_a new_fail_id=[idx for (idx,val) in enumerate(x_intial[:,t]) if val>=1] fail_scale[t]=fail_scale[t-1]+len(new_fail_id) fail_node[new_fail_id]=1 x_intial[new_fail_id,t]=x_intial[new_fail_id,t]+r print(H[i,t]) print(fail_node) print(x_intial) plt.plot(fail_scale) plt.show()
这是一个使用 Python 编写的网络模型,使用了 networkx 库来构建网络。代码中从 csv 文件中读取了网络的边,然后使用 from_pandas_edgelist 方法将边转化为图。接着定义了一个 f(x) 函数,对应于网络中每个节点的失效概率。代码中还定义了一些变量和参数,如节点个数 n、失效概率增长率 r、节点失效后的修复时间 step 等。接下来的循环中,初始化了网络中每个节点的失效概率,并定义了一些辅助变量。然后对网络进行模拟,计算每个节点的失效概率,并记录失效节点的数量。最后,将失效节点的数量用 matplotlib 绘制成图表。
修改class arcnode: def __init__(self, adjvex, weight, link=None): self.adjvex = adjvex self.weight = weight self.link = link class vexnode: def __init__(self, data, first_arc=None): self.data = data self.first_arc = first_arc class Graph: def __init__(self): self.vex_list = [] self.vex_num = 0 self.edge_num = 0 # 请在这里填写答案 def addVertex(self, vex_val): new_vertex = vexnode(vex_val) self.vex_list.append(new_vertex) self.vex_num += 1 def addEdge(self, f, t, cost=0): if f not in self.vex_list: nv = self.addVertex(f) # 如果起始顶点不存在,则将其添加到图中 if t not in self.vex_list: nv = self.addVertex(t) # 如果目标顶点不存在,则将其添加到图中 # 无向图添加双向边 self.vex_list[f].addNeighbor(self.vex_list[t], cost) # 将目标顶点及其权重添加到起始顶点的 connectedTo 字典中 self.vex_list[t].addNeighbor(self.vex_list[f], cost) # 有向图只添加一条边 # 请在这里填写答案 def print_graph(self): for i in range(self.vex_num): print(self.vex_list[i].data, end="->") cur = self.vex_list[i].first_arc while cur: print("adj:{},weight:{}".format(cur.adjvex, cur.weight), end="->") cur = cur.link print('None') if __name__ == "__main__": g = Graph() s = input() for vertex in s: g.addVertex(vertex) g.addEdge(0, 1, 11) g.addEdge(0, 2, 55) g.addEdge(2, 3, 88) g.addEdge(0, 3, 33) g.addEdge(1, 2, 44) g.print_graph()
class arcnode:
def __init__(self, adjvex, weight, link=None):
self.adjvex = adjvex
self.weight = weight
self.link = link
class vexnode:
def __init__(self, data, first_arc=None):
self.data = data
self.first_arc = first_arc
class Graph:
def __init__(self):
self.vex_list = []
self.vex_num = 0
self.edge_num = 0
def addVertex(self, vex_val):
new_vertex = vexnode(vex_val)
self.vex_list.append(new_vertex)
self.vex_num += 1
def addEdge(self, f, t, cost=0):
if f not in range(self.vex_num):
raise ValueError("Invalid Starting Vertex")
if t not in range(self.vex_num):
raise ValueError("Invalid Ending Vertex")
new_arc = arcnode(t, cost)
new_arc.link = self.vex_list[f].first_arc
self.vex_list[f].first_arc = new_arc
self.edge_num += 1
def print_graph(self):
for i in range(self.vex_num):
print(self.vex_list[i].data, end="->")
cur = self.vex_list[i].first_arc
while cur:
print("adj:{},weight:{}".format(cur.adjvex, cur.weight), end="->")
cur = cur.link
print('None')
if __name__ == "__main__":
g = Graph()
s = input()
for vertex in s:
g.addVertex(vertex)
g.addEdge(0, 1, 11)
g.addEdge(0, 2, 55)
g.addEdge(2, 3, 88)
g.addEdge(0, 3, 33)
g.addEdge(1, 2, 44)
g.print_graph()
阅读全文