batchsize = 32 inputdata = torch.randn(batchsize, 140, 4096).to(device) Reshape input…
- User
batch_size = 32 input_data = torch.randn(batch_size, 140, 4096).to(device)
Reshape input data to (batch_size * 140, 4096)
hs2d = input_data.view(-1, 4096)
hs2dn = torch.nn.functional.normalize(hs2d, p=2, dim=1) Q, R = torch.linalg.qr(hs2dn.T)
Dimensionality reduction using the first k columns of Q
k = 5 hs2d_reduced = Q[:, :k].T @ hs2dn.T hs2d_reduced = torch.nn.functional.normalize(hs2d_reduced, p=2, dim=1)
Find feature importance and top features
feature_importance = torch.diag(R) top_features = torch.argsort(feature_importance, descending=True)[:10]
Calculate distances from each data point to each top feature
distances = torch.cdist(hs2d_reduced.T, hs2d_reduced[:, top_features].T)
Reshape distances back to (batch_size, 140, 10)
distances = distances.view(batch_size, 140, 10)
input_data = torch.randn(32, 140, 4096).to(device)
print("Input shape:", input_data.shape) print("hs2d shape:", hs2d.shape) print("hs2dn shape:", hs2dn.shape) print("Q shape:", Q.shape) print("R shape:", R.shape) print("hs2d_reduced shape:", hs2d_reduced.shape) print("Distances shape:", distances.shape)
Получаем размеры матрицы
rows, cols = vis_np.shape
Создаем сетку координат. Для оси X используем номера строк, для оси Y - номера столбцов
X, Y = np.meshgrid(range(cols), range(rows)) fig = plt.figure(figsize=(14, 10)) ax = fig.add_subplot(111, projection='3d') ax.scatter(
Выведи 3D график, каждая группа окрашена отдельно под свой кластер
- Assistant
Assuming
clustersis an array of cluster assignments for each data pointCreate a colormap for coloring each cluster differently
colors = plt.cm.tab20(np.linspace(0, 1, len(np.unique(clusters)))
fig = plt.figure(figsize=(14, 10)) ax = fig.add_subplot(111, projection='3d')
for i, cluster in enumerate(np.unique(clusters)): cluster_indices = np.where(clusters == cluster)[0] ax.scatter(hs2d_reduced[cluster_indices, 0], hs2d_reduced[cluster_indices, 1], hs2d_reduced[cluster_indices, 2], c=colors[i], label=f'Cluster {cluster}')
ax.set_xlabel('Principal Component 1') ax.set_ylabel('Principal Component 2') ax.set_zlabel('Principal Component 3') ax.legend() plt.show()