def main(): num_node = 28 * 28 bm = BoltzmanMachine(num_node, num_hidden=100, lr=0.001) images = load_mnist(normalize=False)["train_images"][:10] # images = (images > 0.5).astype(np.int) num_iter = 1000 for i in range(num_iter): print(i) target = np.random.choice(len(images)) x_sampled = images[target] bm.update(x_sampled) #noised = noise(images, 0.1) noised = images.copy() fig = plt.figure() H = np.floor(np.sqrt(len(noised))) W = 2 * np.ceil(np.sqrt(len(noised))) for i in range(len(noised)): ax = fig.add_subplot(H, W, 2 * i + 1) ax.imshow(noised[i].reshape(28, 28)) mean = bm.gibss_xhx(noised[i], 1000) ax = fig.add_subplot(H, W, 2 * i + 2) ax.imshow(mean.reshape(28, 28)) plt.show()
10分类 采用OvR的方法来训练 fisher new """ import torch as tc import loadmnist as lm import linage_classify #设置cuda device if tc.cuda.is_available(): device = tc.device('cuda', 0) else: device = tc.device('cpu') #载入MNIST数据集 img, label = lm.load_mnist("./data", "train", 'tc') test, tl = lm.load_mnist("./data", "t10k", 'tc') #数据转为cuda对象 img = img.to(device) #把MNIST数据集的标签分类10个二分类器,0为正类,1为负类 filter_vec = tc.tensor(range(10)) filter_mat = filter_vec.repeat(label.size()[0], 1).byte() label_mat = tc.clamp(label.repeat(10, 1) - filter_mat.t(), 0, 1).cuda() #初始化线性分类器,fisher分类器 lcm = linage_classify.FisherClassify() ##lcm.cuda()##使用cuda #训练所有的10个分类器并将结果存入y_hat中
import tensorflow as tf import loadmnist as lm tx,ty = lm.load_mnist('C:\\Users\\Zy\\Desktop\\MNIST','t10k') saver = tf.train.import_meta_graph('saves/classify3-40000.meta') graph = tf.get_default_graph() def Test(input_x,input_y): global graph if len(input_x) == len(input_y) and len(input_x) > 0: x = graph.get_tensor_by_name('x:0') y = graph.get_tensor_by_name('y:0') keep_prob = graph.get_tensor_by_name('keep_prob:0') accuracy = graph.get_tensor_by_name('accuracy:0') pred_pos = graph.get_tensor_by_name('pred_pos:0') with tf.Session() as sess: saver.restore(sess, tf.train.latest_checkpoint('saves/')) if len(input_x) == 1: rs = sess.run(pred_pos, feed_dict={x: input_x, y: input_y, keep_prob: 1}) print(input_y) print('result : %d' % (rs[0]+1)) else: rs = sess.run(accuracy, feed_dict={x: input_x, y: input_y, keep_prob: 1}) print('data(%d) : ac is %f'%(len(input_x),rs*100)) else: print('wrong format you input')