供个人学习记录,来源于:
https://github.com/machinelearningmindset/TensorFlow-Course#why-use-tensorflow
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn import datasets
from tensorflow.python.framework import ops
from tensorflow.examples.tutorials.mnist import input_data
from sklearn.decomposition import PCA
batch_size = 50
num_steps = 1000
log_steps = 50
is_evaluation = True
gamma = -15.0
initial_learning_rate = 0.01
def cross_class_label_fn(A):
"""
This function take the matrix of size (num_classes, batch_size) and return the cross-class label matrix
in which Yij are the elements where i,j are class indices.
:param A: The input matrix of size (num_classes, batch_size).
:return: The output matrix of size (num_classes, batch_size, batch_size).
"""
label_class_i = tf.reshape(A, [num_classes, 1, batch_size])
label_class_j = tf.reshape(label_class_i, [num_classes, batch_size, 1])
returned_mat = tf.matmul(label_class_j, label_class_i)
return returned_mat
# Compute SVM loss.
def loss_fn(alpha, label_placeholder):
term_1 = tf.reduce_sum(alpha)
alpha_cross = tf.matmul(tf.transpose(alpha), alpha) #shape(batch_size,batch_size)
cross_class_label = cross_class_label_fn(label_placeholder) #shape(num_classes,batch_si