105 lines
3.6 KiB
Python
105 lines
3.6 KiB
Python
#
|
||
# import numpy as np
|
||
# import tensorflow as tf
|
||
# import networkx as nx
|
||
# from scipy.sparse import coo_matrix,vstack
|
||
|
||
def adj_to_bias(adj, sizes, nhood=1): #输入adj,输出均为array数据。adj(1,node,feature)
|
||
nb_graphs = adj.shape[0]
|
||
# 随机生成array,shape=(adj.shape,)
|
||
mt = np.empty(adj.shape)
|
||
for g in range(nb_graphs): # 2708
|
||
mt[g] = np.eye(adj.shape[1])
|
||
for _ in range(nhood):
|
||
mt[g] = np.matmul(mt[g], (adj[g] + np.eye(adj.shape[1])))
|
||
for i in range(sizes[g]):
|
||
for j in range(sizes[g]):
|
||
if mt[g][i][j] > 0.0:
|
||
mt[g][i][j] = 1.0
|
||
return -1e9 * (1.0 - mt)
|
||
|
||
|
||
# batch_sets = tf.constant([[1,2,4,5], [2,3,1,2], [3,4,4,1]], dtype=tf.int32)
|
||
# top_k_values, top_k_indices = tf.math.top_k(batch_sets, 2)
|
||
# row_indices = tf.tile(tf.expand_dims(tf.range(3), axis=1), [1, 2])
|
||
# combined_indices = tf.stack([row_indices, top_k_indices], axis=2)
|
||
# combined_indices = tf.reshape(combined_indices, [-1,2])
|
||
# binary_data = tf.zeros_like(batch_sets, dtype=tf.float32)
|
||
# updates = tf.ones(combined_indices.shape[0], dtype=tf.float32)
|
||
# binary_data = tf.tensor_scatter_nd_update(binary_data, combined_indices, updates)
|
||
|
||
import tensorflow as tf
|
||
#
|
||
# # 定义一个函数,它接受一个张量作为输入,并返回一个新的张量
|
||
# def my_function(x):
|
||
# t = tf.where(tf.equal(x, 1))
|
||
# t = tf.squeeze(t, axis=[1])
|
||
# z = tf.where(tf.equal(x, 0))
|
||
# z = tf.squeeze(z, axis=[1])
|
||
# grid_tensor1, grid_tensor2 = tf.meshgrid(t, z)
|
||
# cartesian_product = tf.stack([grid_tensor1, grid_tensor2], axis=-1)
|
||
# output_tensor = tf.reshape(cartesian_product, [-1, 2])
|
||
#
|
||
# return output_tensor
|
||
#
|
||
# # 创建一个张量数组
|
||
# tensors = tf.constant([[1, 0, 1, 0, 0],
|
||
# [1, 0, 0, 1, 0]])
|
||
#
|
||
# # 使用tf.map_fn应用my_function到每个元素
|
||
# pairs = tf.map_fn(my_function, tensors,dtype=tf.int64)
|
||
#
|
||
# # 输出结果
|
||
# print("Original Tensors:")
|
||
# print(tensors)
|
||
# print("Result after applying function:")
|
||
# print(pairs)
|
||
|
||
nums = tf.constant([[5.0, -1.0, 5.0, -1.0, -2.0],[1.0, -4.0, -5.0, 1.0, -2.0]])
|
||
# # labels = tf.constant([[1, 0, 1, 0, 0], [1, 0, 0, 0, 0]], dtype=tf.int32)
|
||
# # #
|
||
# # # def subtract_data_pairs(index_pairs, data_tensor):
|
||
# # #
|
||
# # # first_data = tf.gather(data_tensor, index_pairs[:, 0])
|
||
# # # second_data = tf.gather(data_tensor, index_pairs[:, 1])
|
||
# # #
|
||
# # # # 执行数据对的减法
|
||
# # # subtracted_data = first_data - second_data
|
||
# # #
|
||
# # # return subtracted_data
|
||
# # #
|
||
# # # # 调用函数
|
||
# # # result = tf.map_fn(lambda x: subtract_data_pairs(x[0], x[1]), (pairs,nums),dtype=tf.int32)
|
||
# # # # 打印结果
|
||
# # # print(result.numpy())
|
||
# #
|
||
# def masked_bpr_loss(index_pairs, logits):
|
||
# """Self-defined bpr loss function."""
|
||
#
|
||
# def subtract_data_pairs(index_pairs, data_tensor):
|
||
# first_data = tf.gather(data_tensor, index_pairs[:, 0])
|
||
# second_data = tf.gather(data_tensor, index_pairs[:, 1])
|
||
# subtracted_data = first_data - second_data
|
||
# return subtracted_data
|
||
#
|
||
# diff = tf.map_fn(lambda x: subtract_data_pairs(x[0], x[1]), (index_pairs, logits), dtype=tf.float32)
|
||
# # diff = tf.reshape(diff, [-1])
|
||
# print(diff)
|
||
#
|
||
#
|
||
# masked_bpr_loss(pairs, nums)
|
||
#
|
||
#
|
||
# tf.gather()
|
||
# index = tf.constant([[1, 2], [0, 3]])
|
||
# gathered_elements = tf.gather_nd(nums, index)
|
||
# print(gathered_elements)
|
||
|
||
import numpy as np
|
||
one = np.loadtxt('C:/Users/zhou/Desktop/github_mongoDB/one_index_1000.txt', dtype=int)
|
||
zero = np.loadtxt('C:/Users/zhou/Desktop/github_mongoDB/zero_index_1000.txt', dtype=int)
|
||
t = np.stack((one, zero))
|
||
print(t[0].shape,t[1].shape)
|
||
print(t.shape)
|
||
print(t[0]==zero)
|