TensorFlow学习之路(1): correct = tf.nn.in_top_k(logits, labels, k)

本文详细解析了 TensorFlow 中 tf.nn.in_top_k 和 tf.nn.top_k 函数的工作原理及使用方法,阐述了如何通过这两个函数来判断预测结果是否包含在真实标签内,并提供了具体的示例说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天写代码时,发现以前对Top_k理解有误,现在,重新整理对此的理解:

correct = tf.nn.in_top_k(logits, labels, k)

其中:

    logits: a tensor  of shape [batch_size, NUM_CLASSES]
    labels: a tensor of shape [batch_size]

理解:
1.对于logits的某行logits[i],找到其前k个最大的预测值的index_0, .., index_k-1,
如果发现对应的labels[i]在{index_0, …, index_k-1}, 则返回True.(大致这个意思)
2.当k=1时,等价于tf.equal(logits, labels)。但是,equal()函数中的logits和labels的shape必须一样。因此,通过read_data_sets(…, one_hot=False,…)读取数据时,必须使得one_hot=Ture(默认为False).

下面为源码中对此的解释:

This outputs a batch_size bool array, an entry out[i] is true if the prediction for the target class is among the top k predictions among all predictions for example i.

注意:
tf.nn.top_k(input, k=1, sorted=sorted, name)
在top_k()函数中,返回的是两个值:
values: input中最后一维部分的前k个最大的值。
indices:与values中各个值对应的索引。
(sorted默认为1, 即根据输出values进行降序输出)

对下面代码进行改错 import tensorflow.compat.v1 as tf tf.compat.v1.disable_eager_execution() from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data', one_hot=True) num_classes = 10 input_size = 784 hidden_units_size = 30 batch_size = 100 training_iterations = 10000 X = tf.placeholder(tf.float32, [None, input_size]) Y = tf.placeholder(tf.float32, [None, num_classes]) W1 = tf.Variable(tf.random_normal([input_size, hidden_units_size],stddev = 0.1)) B1 = tf.Variable(tf.constant([hidden_units_size])) W2 = tf.Variable(tf.random_normal ([hidden_units_size,num_classes],stddev = 0.1)) B2 = tf.Variable(tf.constant(0.1), [num_classes]) hidden_opt = tf.matmul(X, W1) + B1 hidden_opt = tf.nn.relu(hidden_opt) final_opt = tf.matmul(hidden_opt, W2) + B2 final_opt = tf.nn.relu(final_opt) loss1 = tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=final_opt) loss = tf.reduce_mean(loss1) opt = tf.train.GradientDescentOptimizer(0.05).minimize(loss) init = tf.global_variables_initializer() correct_prediction = tf.equal(tf.argmax(Y,1), tf.argmax(final_opt,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) sess = tf.Session() sess.run(init) for i in range(training_iterations): batch = mnist.train.next_batch(batch_size) batch_input = batch[0] batch_labels = batch[1] train_loss = sess.run([opt, loss], feed_dict={X: batch_input, Y: batch_labels}) if i % 100 == 0: train_accuracy = accuracy.eval (session = sess, feed_dict={X: batch_input, Y: batch_labels}) print("step %d, training accuracy %g" % (i, train_accuracy))
04-01
请查阅以下卷积神经网络代码,给出尝试运行并改良后的结果import tensorflow as tf import numpy as np from captcha.image import ImageCaptcha from PIL import Image import random import matplotlib.pyplot as plt number = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] ALPHABET = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] def random_captcha_text(char_set=number + alphabet + ALPHABET, captcha_size=4): captcha_text = [] for i in range(captcha_size): c = random.choice(char_set) captcha_text.append(c) return captcha_text def gen_captcha_text_and_image(i=0): # 创建图像实例对象 image = ImageCaptcha() # 随机选择4个字符 captcha_text = random_captcha_text() # array 转化为 string captcha_text = ''.join(captcha_text) # 生成验证码 captcha = image.generate(captcha_text) if i % 100 == 0: image.write(captcha_text, "D:/justin/captcha/image/" + captcha_text + '.jpg') captcha_image = Image.open(captcha) captcha_image = np.array(captcha_image) return captcha_text, captcha_image def convert2gray(img): if len(img.shape) > 2: gray = np.mean(img, -1) return gray else: return img # 文本转向量 def text2vec(text): text_len = len(text) if text_len > MAX_CAPTCHA: raise ValueError('验证码最长4个字符') vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN) def char2pos(c): if c == '_': k = 62 return k k = ord(c) - 48 if k > 9: k = ord(c) - 55 if k > 35: k = ord(c) - 61 if k > 61: raise ValueError('No Map') return k for i, c in enumerate(text): idx = i * CHAR_SET_LEN + char2pos(c) vector[idx] = 1 return vector # 向量转回文本 def vec2text(vec): char_pos = vec[0] text = [] for i, c in enumerate(char_pos): char_idx = c % CHAR_SET_LEN if char_idx < 10: char_code = char_idx + ord('0') elif char_idx < 36: char_code = char_idx - 10 + ord('A') elif char_idx < 62: char_code = char_idx - 36 + ord('a') elif char_idx == 62: char_code = ord('_') else: raise ValueError('error') text.append(chr(char_code)) return "".join(text) # 生成一个训练batch def get_next_batch(batch_size=64): batch_x = np.zeros([batch_size, IMAGE_HEIGHT * IMAGE_WIDTH]) batch_y = np.zeros([batch_size, MAX_CAPTCHA * CHAR_SET_LEN]) def wrap_gen_captcha_text_and_image(i): while True: text, image = gen_captcha_text_and_image(i) if image.shape == (60, 160, 3): return text, image for i in range(batch_size): text, image = wrap_gen_captcha_text_and_image(i) image = convert2gray(image) batch_x[i, :] = image.flatten() / 255 batch_y[i, :] = text2vec(text) return batch_x, batch_y # 定义CNN def crack_captcha_cnn(w_alpha=0.01, b_alpha=0.1): x = tf.reshape(X, shape=[-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1]) w_c1 = tf.Variable(w_alpha * tf.random_normal([3, 3, 1, 32])) b_c1 = tf.Variable(b_alpha * tf.random_normal([32])) conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, w_c1, strides=[1, 1, 1, 1], padding='SAME'), b_c1)) conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') conv1 = tf.nn.dropout(conv1, rate=1 - keep_prob) w_c2 = tf.Variable(w_alpha * tf.random_normal([3, 3, 32, 64])) b_c2 = tf.Variable(b_alpha * tf.random_normal([64])) conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv1, w_c2, strides=[1, 1, 1, 1], padding='SAME'), b_c2)) conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') conv2 = tf.nn.dropout(conv2, rate=1 - keep_prob) w_c3 = tf.Variable(w_alpha * tf.random_normal([3, 3, 64, 64])) b_c3 = tf.Variable(b_alpha * tf.random_normal([64])) conv3 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv2, w_c3, strides=[1, 1, 1, 1], padding='SAME'), b_c3)) conv3 = tf.nn.max_pool(conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') conv3 = tf.nn.dropout(conv3, rate=1 - keep_prob) w_d = tf.Variable(w_alpha * tf.random_normal([8 * 20 * 64, 1024])) b_d = tf.Variable(b_alpha * tf.random_normal([1024])) dense = tf.reshape(conv3, [-1, w_d.get_shape().as_list()[0]]) dense = tf.nn.relu(tf.add(tf.matmul(dense, w_d), b_d)) dense = tf.nn.dropout(dense, rate=1 - keep_prob) w_out = tf.Variable(w_alpha * tf.random_normal([1024, MAX_CAPTCHA * CHAR_SET_LEN])) b_out = tf.Variable(b_alpha * tf.random_normal([MAX_CAPTCHA * CHAR_SET_LEN])) out = tf.add(tf.matmul(dense, w_out), b_out) return out # 训练 def train_crack_captcha_cnn(): output = crack_captcha_cnn() loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output, labels=Y)) # 计算损失 optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) # 计算梯度 predict = tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]) # 目标预测 max_idx_p = tf.argmax(predict, 2) # 目标预测最大值 max_idx_l = tf.argmax(tf.reshape(Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) # 真实标签最大值 correct_pred = tf.equal(max_idx_p, max_idx_l) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # 准确率 saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) step = 0 while True: batch_x, batch_y = get_next_batch(64) _, loss_ = sess.run([optimizer, loss], feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.75}) print(step, loss_) if step % 100 == 0: batch_x_test, batch_y_test = get_next_batch(100) acc = sess.run(accuracy, feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1.}) print(step, "准确率:",acc) if acc > 0.85: saver.save(sess, "D:/justin/captcha/model/85", global_step=step) step += 1 def crack_captcha(captcha_image, output): saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) # 获取训练后的参数 checkpoint = tf.train.get_checkpoint_state("model") if checkpoint and checkpoint.model_checkpoint_path: saver.restore(sess, checkpoint.model_checkpoint_path) print("Successfully loaded:", checkpoint.model_checkpoint_path) else: print("Could not find old network weights") predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) text_list = sess.run(predict, feed_dict={X: [captcha_image], keep_prob: 1}) text = vec2text(text_list) return text if __name__ == '__main__': train = 1 # 0: 训练 1: 预测 if train == 0: number = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] ALPHABET = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] text, image = gen_captcha_text_and_image() print("验证码图像channel:", image.shape) # 图像大小 IMAGE_HEIGHT = 60 IMAGE_WIDTH = 160 MAX_CAPTCHA = len(text) print("验证码文本最长字符数", MAX_CAPTCHA) # 文本转向量 char_set = number + alphabet + ALPHABET + ['_'] # 如果验证码长度小于4, '_'用来补齐 CHAR_SET_LEN = len(char_set) # placeholder占位符 X = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT * IMAGE_WIDTH]) Y = tf.placeholder(tf.float32, [None, MAX_CAPTCHA * CHAR_SET_LEN]) keep_prob = tf.placeholder(tf.float32) train_crack_captcha_cnn() # 预测时需要将训练的变量初始化 if train == 1: # 自然计数 step = 0 # 正确预测计数 rightCnt = 0 # 设置测试次数 count = 10 number = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] ALPHABET = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] IMAGE_HEIGHT = 60 IMAGE_WIDTH = 160 char_set = number + alphabet + ALPHABET + ['_'] CHAR_SET_LEN = len(char_set) MAX_CAPTCHA = 4 # placeholder占位符 X = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT * IMAGE_WIDTH]) Y = tf.placeholder(tf.float32, [None, MAX_CAPTCHA * CHAR_SET_LEN]) keep_prob = tf.placeholder(tf.float32) output = crack_captcha_cnn() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # 获取训练后参数路径 checkpoint = tf.train.get_checkpoint_state("model") if checkpoint and checkpoint.model_checkpoint_path: saver.restore(sess, checkpoint.model_checkpoint_path) print("Successfully loaded:", checkpoint.model_checkpoint_path) else: print("Could not find old network weights.") while True: text, image = gen_captcha_text_and_image() f = plt.figure() ax = f.add_subplot(111) ax.text(0.1, 0.9,text, ha='center', va='center', transform=ax.transAxes) plt.imshow(image) plt.show() image = convert2gray(image) image = image.flatten() / 255 predict = tf.math.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) text_list = sess.run(predict, feed_dict={X: [image], keep_prob: 1}) predict_text = vec2text(text_list) predict_text = crack_captcha(image, output) print("step:{} 真实值: {} 预测: {} 预测结果: {}".format(str(step), text, predict_text, "正确" if text.lower() == predict_text.lower() else "错误")) if text.lower() == predict_text.lower(): rightCnt += 1 if step == count - 1: print("测试总数: {} 测试准确率: {}".format(str(count), str(rightCnt / count))) break step += 1
最新发布
06-20
# -*- coding: utf-8 -*- import numpy as np import torch import torch.nn as nn import torch.optim as optim from pypuf.simulation import ArbiterPUF from pypuf.io import random_inputs from sklearn.model_selection import train_test_split # ========== 1. 定义神经网络模型 ========== class APUFModel(nn.Module): def __init__(self, input_size): super(APUFModel, self).__init__() self.fc = nn.Sequential( nn.Linear(input_size, 128), nn.ReLU(), nn.Linear(128, 64), nn.ReLU(), nn.Linear(64, 1), nn.Sigmoid() ) def forward(self, x): return self.fc(x) # ========== 2. 权重保存函数 ========== def save_weights_to_csv(weights, filename): """保存模型权重到CSV文件""" if isinstance(weights, torch.Tensor): weights = weights.detach().numpy() np.savetxt(filename, weights, delimiter=',') # ========== 3. 创建APUF实例 ========== n = 64 apuf = ArbiterPUF(n=n, noisiness=0, seed=1) # ========== 4. 生成CRP数据集 ========== num_crps = 100000 challenges = random_inputs(n=n, N=num_crps, seed=2) responses = apuf.eval(challenges) # 转换为PyTorch Tensor X = challenges.reshape(num_crps, n).astype(np.float32) y = ((responses + 1) // 2).astype(np.float32).reshape(-1, 1) # ========== 5. 划分数据集 ========== X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) # 转换为PyTorch Dataset train_dataset = torch.utils.data.TensorDataset( torch.from_numpy(X_train), torch.from_numpy(y_train) ) test_dataset = torch.utils.data.TensorDataset( torch.from_numpy(X_test), torch.from_numpy(y_test) ) # ========== 6. 初始化模型和优化器 ========== model = APUFModel(input_size=n) criterion = nn.BCELoss() optimizer = optim.Adam(model.parameters(), lr=0.001) # ========== 7. 训练前保存初始权重 ========== initial_weights = model.fc[0].weight.data.clone() save_weights_to_csv(initial_weights, "initial_weights.csv") # ========== 8. 训练循环 ========== batch_size = 512 train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=batch_size, shuffle=True ) test_loader = torch.utils.data.DataLoader( test_dataset, batch_size=batch_size ) best_accuracy = 0.0 early_stop = False for epoch in range(100): # 最大100个epoch if early_stop: break # 训练阶段 model.train() for inputs, labels in train_loader: optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() # 验证阶段 model.eval() correct = 0 total = 0 with torch.no_grad(): for inputs, labels in test_loader: outputs = model(inputs) predicted = (outputs > 0.5).float() total += labels.size(0) correct += (predicted == labels).sum().item() accuracy = correct / total print(f"Epoch {epoch+1}: 准确率 {accuracy:.2%}") # 准确率达标则停止 if accuracy > 0.90: print(f"达到90%准确率,提前终止训练!") early_stop = True # ========== 9. 训练后保存最终权重 ========== final_weights = model.fc[0].weight.data.clone() save_weights_to_csv(final_weights, "final_weights.csv") # ========== 10. 验证最终结果 ========== model.eval() with torch.no_grad(): test_outputs = model(torch.from_numpy(X_test)) test_predicted = (test_outputs > 0.5).float() final_accuracy = (test_predicted.numpy() == y_test).mean() print(f"\n最终测试准确率: {final_accuracy:.2%}") print("初始权重已保存至 initial_weights.csv") print("最终权重已保存至 final_weights.csv") 这些是使用pytorch来实现的,现在将pytorch替换为tensorflow来实现相同的功能
03-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值