tensorflow-base_operations

本文介绍了TensorFlow中的基本操作,包括常量定义、变量运算、占位符使用及矩阵运算等核心概念。通过具体示例展示了如何在TensorFlow环境中执行加法、乘法等基本数学运算。

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

# -*- coding: utf-8 -*-
import tensorflow as tf
# 基本的常量操作,通过构造函数返回值 定义值的操作operations
a = tf.constant(2)
b = tf.constant(3)

# 启动默认 图形实例sess
#支持运算
with tf.Session() as sess:
print("a: %i" % sess.run(a), "b: %i" % sess.run(b))
print("a * b = %i" % sess.run(a*b))
print("a * b = %i" % sess.run(a*4))
'''
# 基本操作之 将值以图像形式输出
# 构造函数返回的值为输出的对象
# 通过Session().run()定义为输出对象
# tf图像对象 输入
a = tf.pleaseholder(tf.int16) # pleaseholder 买主
b = tf.pleaseholder(tf.int16)
'''
# 定义值
add = tf.add(a, b)
mul = tf.multiply(a, b)

# 启动tf的图像实例
with tf.Session() as sess:
# 进行每个操作op 输入
print('Addition with variable: %i' %(sess.run(add, feed_dict={a:2,b:3})))
print('Addition with variable: %i' %(sess.run(add)))
print('multiplication with variable: %i' % sess.run(mul, feed_dict={a:2,b:3}))
print('multiplication with variable: %i' % sess.run(mul, feed_dict={a:3,b:3})) # feed_dict传入的参数
print('multiplication with variable: %i' % sess.run(mul, feed_dict={a:3,b:3})) # key value

# 创建一个产生1x2矩阵matrix的常量操作
# 在图形中添加一个节点
'''在线性空间中选定基之后,向量刻画对象,矩阵刻画对象的运动,用矩阵与向量的乘法施加运动。
若矩阵A与B是同一个线性变换的两个不同的描述(之所以会不同,是因为选定了不同的基,也就是选定了不同的坐标系),
则一定能找到一个非奇异矩阵P,使得A、B之间满足这样的关系:A = P-1BP P的负一次方
矩阵的本质是运动的描述
矩阵是线性空间里跃迁的描述
矩阵是线性空间里的变换的描述

'''
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[3.], [3.]])
product = tf.matmul(matrix1, matrix2)

with tf.Session() as sess:
result1 = sess.run(matrix1)
result2 = sess.run(matrix2)
result3 = sess.run(product)
print(result1, result2, '--', result3)
posted on 2018-06-04 11:00 .Tang 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/tangpg/p/9132267.html

2025-06-22 18:56:53.192710: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2025-06-22 18:56:54.136866: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2025-06-22 18:56:56,467 - INFO - 加载并增强数据集: augmented_data 2025-06-22 18:56:56,560 - INFO - 原始数据集: 150 张图片, 5 个类别 2025-06-22 18:56:56,565 - INFO - 类别 book: 30 张原始图像 2025-06-22 18:56:56,989 - INFO - 类别 cup: 30 张原始图像 2025-06-22 18:56:57,403 - INFO - 类别 glasses: 30 张原始图像 2025-06-22 18:56:57,820 - INFO - 类别 phone: 30 张原始图像 2025-06-22 18:56:58,248 - INFO - 类别 shoe: 30 张原始图像 2025-06-22 18:56:58,859 - INFO - 增强后数据集: 450 张图片 2025-06-22 18:56:58,954 - INFO - 构建优化的迁移学习模型... 2025-06-22 18:56:58.959007: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. Model: "functional" ┌─────────────────────┬───────────────────┬────────────┬───────────────────┐ │ Layer (type) │ Output Shape │ Param # │ Connected to │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ input_layer_1 │ (None, 224, 224, │ 0 │ - │ │ (InputLayer) │ 3) │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ efficientnetb0 │ (None, 7, 7, │ 4,049,571 │ input_layer_1[0]… │ │ (Functional) │ 1280) │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ global_average_poo… │ (None, 1280) │ 0 │ efficientnetb0[0… │ │ (GlobalAveragePool… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dense (Dense) │ (None, 512) │ 655,872 │ global_average_p… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dense_1 (Dense) │ (None, 1280) │ 656,640 │ dense[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ multiply (Multiply) │ (None, 1280) │ 0 │ global_average_p… │ │ │ │ │ dense_1[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dense_2 (Dense) │ (None, 512) │ 655,872 │ multiply[0][0] │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ batch_normalization │ (None, 512) │ 2,048 │ dense_2[0][0] │ │ (BatchNormalizatio… │ │ │ │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dropout (Dropout) │ (None, 512) │ 0 │ batch_normalizat… │ ├─────────────────────┼───────────────────┼────────────┼───────────────────┤ │ dense_3 (Dense) │ (None, 5) │ 2,565 │ dropout[0][0] │ └─────────────────────┴───────────────────┴────────────┴───────────────────┘ Total params: 6,022,568 (22.97 MB) Trainable params: 1,971,973 (7.52 MB) Non-trainable params: 4,050,595 (15.45 MB) 2025-06-22 18:56:59,882 - INFO - 开始高级训练策略... 2025-06-22 18:56:59,882 - INFO - 阶段1: 冻结基础模型训练 Epoch 1/50 23/23 ━━━━━━━━━━━━━━━━━━━━ 16s 442ms/step - accuracy: 0.2001 - loss: 1.7270 - val_accuracy: 0.2000 - val_loss: 1.6104 - learning_rate: 1.0000e-04 Epoch 2/50 23/23 ━━━━━━━━━━━━━━━━━━━━ 8s 335ms/step - accuracy: 0.1486 - loss: 1.7114 - val_accuracy: 0.2000 - val_loss: 1.6100 - learning_rate: 1.0000e-04 Epoch 3/50 23/23 ━━━━━━━━━━━━━━━━━━━━ 8s 332ms/step - accuracy: 0.2337 - loss: 1.7239 - val_accuracy: 0.2000 - val_loss: 1.6117 - learning_rate: 1.0000e-04 Epoch 4/50 23/23 ━━━━━━━━━━━━━━━━━━━━ 8s 335ms/step - accuracy: 0.2558 - loss: 1.6466 - val_accuracy: 0.2000 - val_loss: 1.6104 - learning_rate: 1.0000e-04 Epoch 5/50 23/23 ━━━━━━━━━━━━━━━━━━━━ 0s 270ms/step - accuracy: 0.2281 - loss: 1.6503 Epoch 5: ReduceLROnPlateau reducing learning rate to 4.999999873689376e-05. 23/23 ━━━━━━━━━━━━━━━━━━━━ 8s 367ms/step - accuracy: 0.2271 - loss: 1.6513 - val_accuracy: 0.2111 - val_loss: 1.6118 - learning_rate: 1.0000e-04 Epoch 6/50 23/23 ━━━━━━━━━━━━━━━━━━━━ 8s 333ms/step - accuracy: 0.1899 - loss: 1.6756 - val_accuracy: 0.2000 - val_loss: 1.6112 - learning_rate: 5.0000e-05 Epoch 7/50 23/23 ━━━━━━━━━━━━━━━━━━━━ 8s 333ms/step - accuracy: 0.2394 - loss: 1.6269 - val_accuracy: 0.2000 - val_loss: 1.6128 - learning_rate: 5.0000e-05 Epoch 8/50 23/23 ━━━━━━━━━━━━━━━━━━━━ 0s 266ms/step - accuracy: 0.2041 - loss: 1.7332 Epoch 8: ReduceLROnPlateau reducing learning rate to 2.499999936844688e-05. 23/23 ━━━━━━━━━━━━━━━━━━━━ 8s 333ms/step - accuracy: 0.2042 - loss: 1.7319 - val_accuracy: 0.2000 - val_loss: 1.6103 - learning_rate: 5.0000e-05 Epoch 9/50 23/23 ━━━━━━━━━━━━━━━━━━━━ 8s 355ms/step - accuracy: 0.1765 - loss: 1.6814 - val_accuracy: 0.3333 - val_loss: 1.6107 - learning_rate: 2.5000e-05 2025-06-22 18:58:18,867 - INFO - 阶段2: 微调部分层 Epoch 1/25 23/23 ━━━━━━━━━━━━━━━━━━━━ 18s 460ms/step - accuracy: 0.2374 - loss: 2.4082 - val_accuracy: 0.2000 - val_loss: 1.6107 - learning_rate: 1.0000e-05 Epoch 2/25 23/23 ━━━━━━━━━━━━━━━━━━━━ 9s 390ms/step - accuracy: 0.2021 - loss: 2.2585 - val_accuracy: 0.2000 - val_loss: 1.6112 - learning_rate: 1.0000e-05 Epoch 3/25 23/23 ━━━━━━━━━━━━━━━━━━━━ 9s 375ms/step - accuracy: 0.2259 - loss: 2.3548 - val_accuracy: 0.2111 - val_loss: 1.6121 - learning_rate: 1.0000e-05 Epoch 4/25 23/23 ━━━━━━━━━━━━━━━━━━━━ 0s 307ms/step - accuracy: 0.2416 - loss: 2.0942 Epoch 4: ReduceLROnPlateau reducing learning rate to 4.999999873689376e-06. 23/23 ━━━━━━━━━━━━━━━━━━━━ 9s 374ms/step - accuracy: 0.2405 - loss: 2.1006 - val_accuracy: 0.2000 - val_loss: 1.6127 - learning_rate: 1.0000e-05 Epoch 5/25 23/23 ━━━━━━━━━━━━━━━━━━━━ 9s 377ms/step - accuracy: 0.2053 - loss: 2.1248 - val_accuracy: 0.2000 - val_loss: 1.6136 - learning_rate: 5.0000e-06 Epoch 6/25 23/23 ━━━━━━━━━━━━━━━━━━━━ 9s 382ms/step - accuracy: 0.1995 - loss: 2.2549 - val_accuracy: 0.2000 - val_loss: 1.6150 - learning_rate: 5.0000e-06 Epoch 7/25 23/23 ━━━━━━━━━━━━━━━━━━━━ 0s 305ms/step - accuracy: 0.1949 - loss: 2.1615 Epoch 7: ReduceLROnPlateau reducing learning rate to 2.499999936844688e-06. 23/23 ━━━━━━━━━━━━━━━━━━━━ 9s 373ms/step - accuracy: 0.1962 - loss: 2.1615 - val_accuracy: 0.2000 - val_loss: 1.6165 - learning_rate: 5.0000e-06 Epoch 8/25 23/23 ━━━━━━━━━━━━━━━━━━━━ 9s 375ms/step - accuracy: 0.2320 - loss: 2.1199 - val_accuracy: 0.2000 - val_loss: 1.6186 - learning_rate: 2.5000e-06 Epoch 9/25 23/23 ━━━━━━━━━━━━━━━━━━━━ 9s 378ms/step - accuracy: 0.2379 - loss: 2.1694 - val_accuracy: 0.2000 - val_loss: 1.6204 - learning_rate: 2.5000e-06 2025-06-22 18:59:46,920 - INFO - 训练完成 2025-06-22 18:59:46,920 - INFO - 评估模型... 2025-06-22 18:59:48,600 - INFO - 测试准确率: 20.00% E:\pycharm\study\计算机视觉\物品识别系统.py:313: UserWarning: Glyph 20934 (\N{CJK UNIFIED IDEOGRAPH-51C6}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:313: UserWarning: Glyph 30830 (\N{CJK UNIFIED IDEOGRAPH-786E}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:313: UserWarning: Glyph 29575 (\N{CJK UNIFIED IDEOGRAPH-7387}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:313: UserWarning: Glyph 35757 (\N{CJK UNIFIED IDEOGRAPH-8BAD}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:313: UserWarning: Glyph 32451 (\N{CJK UNIFIED IDEOGRAPH-7EC3}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:313: UserWarning: Glyph 21644 (\N{CJK UNIFIED IDEOGRAPH-548C}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:313: UserWarning: Glyph 39564 (\N{CJK UNIFIED IDEOGRAPH-9A8C}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:313: UserWarning: Glyph 35777 (\N{CJK UNIFIED IDEOGRAPH-8BC1}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:313: UserWarning: Glyph 25439 (\N{CJK UNIFIED IDEOGRAPH-635F}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:313: UserWarning: Glyph 22833 (\N{CJK UNIFIED IDEOGRAPH-5931}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:314: UserWarning: Glyph 20934 (\N{CJK UNIFIED IDEOGRAPH-51C6}) missing from font(s) DejaVu Sans. plt.savefig('training_history.png') E:\pycharm\study\计算机视觉\物品识别系统.py:314: UserWarning: Glyph 30830 (\N{CJK UNIFIED IDEOGRAPH-786E}) missing from font(s) DejaVu Sans. plt.savefig('training_history.png') E:\pycharm\study\计算机视觉\物品识别系统.py:314: UserWarning: Glyph 29575 (\N{CJK UNIFIED IDEOGRAPH-7387}) missing from font(s) DejaVu Sans. plt.savefig('training_history.png') E:\pycharm\study\计算机视觉\物品识别系统.py:314: UserWarning: Glyph 35757 (\N{CJK UNIFIED IDEOGRAPH-8BAD}) missing from font(s) DejaVu Sans. plt.savefig('training_history.png') E:\pycharm\study\计算机视觉\物品识别系统.py:314: UserWarning: Glyph 32451 (\N{CJK UNIFIED IDEOGRAPH-7EC3}) missing from font(s) DejaVu Sans. plt.savefig('training_history.png') E:\pycharm\study\计算机视觉\物品识别系统.py:314: UserWarning: Glyph 21644 (\N{CJK UNIFIED IDEOGRAPH-548C}) missing from font(s) DejaVu Sans. plt.savefig('training_history.png') E:\pycharm\study\计算机视觉\物品识别系统.py:314: UserWarning: Glyph 39564 (\N{CJK UNIFIED IDEOGRAPH-9A8C}) missing from font(s) DejaVu Sans. plt.savefig('training_history.png') E:\pycharm\study\计算机视觉\物品识别系统.py:314: UserWarning: Glyph 35777 (\N{CJK UNIFIED IDEOGRAPH-8BC1}) missing from font(s) DejaVu Sans. plt.savefig('training_history.png') E:\pycharm\study\计算机视觉\物品识别系统.py:314: UserWarning: Glyph 25439 (\N{CJK UNIFIED IDEOGRAPH-635F}) missing from font(s) DejaVu Sans. plt.savefig('training_history.png') E:\pycharm\study\计算机视觉\物品识别系统.py:314: UserWarning: Glyph 22833 (\N{CJK UNIFIED IDEOGRAPH-5931}) missing from font(s) DejaVu Sans. plt.savefig('training_history.png') 2025-06-22 18:59:48,905 - INFO - 训练历史图表已保存到 training_history.png 2025-06-22 18:59:49,390 - INFO - 模型已保存到: optimized_model.keras 2025-06-22 18:59:49,390 - INFO - 执行内存清理... WARNING:tensorflow:From E:\python3.9.13\lib\site-packages\keras\src\backend\common\global_state.py:82: The name tf.reset_default_graph is deprecated. Please use tf.compat.v1.reset_default_graph instead. 2025-06-22 18:59:50,195 - WARNING - From E:\python3.9.13\lib\site-packages\keras\src\backend\common\global_state.py:82: The name tf.reset_default_graph is deprecated. Please use tf.compat.v1.reset_default_graph instead. 2025-06-22 18:59:50,743 - INFO - 内存清理完成 E:\pycharm\study\计算机视觉\物品识别系统.py:355: UserWarning: set_ticklabels() should only be used with a fixed number of ticks, i.e. after set_ticks() or using a FixedLocator. ax2.set_xticklabels(self.class_labels, rotation=45) E:\pycharm\study\计算机视觉\物品识别系统.py:364: UserWarning: Glyph 39044 (\N{CJK UNIFIED IDEOGRAPH-9884}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:364: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:364: UserWarning: Glyph 27010 (\N{CJK UNIFIED IDEOGRAPH-6982}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:364: UserWarning: Glyph 29575 (\N{CJK UNIFIED IDEOGRAPH-7387}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:364: UserWarning: Glyph 31867 (\N{CJK UNIFIED IDEOGRAPH-7C7B}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:364: UserWarning: Glyph 21035 (\N{CJK UNIFIED IDEOGRAPH-522B}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:364: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:364: UserWarning: Glyph 24067 (\N{CJK UNIFIED IDEOGRAPH-5E03}) missing from font(s) DejaVu Sans. plt.tight_layout() E:\pycharm\study\计算机视觉\物品识别系统.py:365: UserWarning: Glyph 39044 (\N{CJK UNIFIED IDEOGRAPH-9884}) missing from font(s) DejaVu Sans. plt.savefig(output_path) E:\pycharm\study\计算机视觉\物品识别系统.py:365: UserWarning: Glyph 27979 (\N{CJK UNIFIED IDEOGRAPH-6D4B}) missing from font(s) DejaVu Sans. plt.savefig(output_path) E:\pycharm\study\计算机视觉\物品识别系统.py:365: UserWarning: Glyph 27010 (\N{CJK UNIFIED IDEOGRAPH-6982}) missing from font(s) DejaVu Sans. plt.savefig(output_path) E:\pycharm\study\计算机视觉\物品识别系统.py:365: UserWarning: Glyph 29575 (\N{CJK UNIFIED IDEOGRAPH-7387}) missing from font(s) DejaVu Sans. plt.savefig(output_path) E:\pycharm\study\计算机视觉\物品识别系统.py:365: UserWarning: Glyph 31867 (\N{CJK UNIFIED IDEOGRAPH-7C7B}) missing from font(s) DejaVu Sans. plt.savefig(output_path) E:\pycharm\study\计算机视觉\物品识别系统.py:365: UserWarning: Glyph 21035 (\N{CJK UNIFIED IDEOGRAPH-522B}) missing from font(s) DejaVu Sans. plt.savefig(output_path) E:\pycharm\study\计算机视觉\物品识别系统.py:365: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from font(s) DejaVu Sans. plt.savefig(output_path) E:\pycharm\study\计算机视觉\物品识别系统.py:365: UserWarning: Glyph 24067 (\N{CJK UNIFIED IDEOGRAPH-5E03}) missing from font(s) DejaVu Sans. plt.savefig(output_path) 2025-06-22 18:59:52,251 - INFO - 预测结果已保存到 prediction_result.png 2025-06-22 18:59:52,252 - INFO - 真实类别: cup
最新发布
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值