tensorflow的基本应用

1  tf.split()

  

f.split(dimension, num_split, input):dimension的意思就是输入张量的哪一个维度,如果是0就表示对第0维度进行切割。num_split就是切割的数量,如果是2就表示输入张量被切成2份,每一份是一个列表。

例如:

  1. import tensorflow as tf;
  2. import numpy as np;
  3. A = [[ 1, 2, 3],[ 4, 5, 6]]
  4. x = tf.split( 1, 3, A)
  5. with tf.Session() as sess:
  6. c = sess.run(x)
  7. for ele in c:
  8. print ele
输出:

[[1]
 [4]]
[[2]
 [5]]
[[3]
 [6]]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow是一个开源软件库,主要用于各种机器学习应用程序的构建和部署。下面是一些TensorFlow的基本操作: 1. 导入TensorFlow库 ```python import tensorflow as tf ``` 2. 创建张量(Tensor) ```python # 创建一个标量(0维张量) x = tf.constant(5) # 创建一个向量 (1维张量) y = tf.constant([1, 2, 3, 4]) # 创建一个矩阵 (2维张量) z = tf.constant([[1, 2], [3, 4]]) ``` 3. 运行图(Graph)并输出结果 ```python with tf.Session() as sess: result = sess.run(x) print(result) ``` 4. 创建占位符(Placeholder) ```python x = tf.placeholder(tf.float32, shape=(None, 2)) y = tf.placeholder(tf.float32, shape=(None, 1)) ``` 5. 定义变量(Variable) ```python W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) ``` 6. 定义模型 ```python y_pred = tf.matmul(x, W) + b ``` 7. 定义损失函数(Loss Function) ```python cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_pred)) ``` 8. 定义优化器(Optimizer) ```python train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) ``` 9. 初始化变量 ```python init = tf.global_variables_initializer() ``` 10. 训练模型 ```python with tf.Session() as sess: sess.run(init) for i in range(num_iterations): # Run one optimization step sess.run(train_step, feed_dict={x: X_train, y_true: y_train}) ``` 以上是一些TensorFlow的基本操作,可以帮助你快速入门TensorFlow。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值