tf.math.equal() 函数讲解

tf.math.equal() 函数讲解

参数:tf.math.equal(a,b),其中a,b可以是tensor或者具体的数字。

作用:如果二者都是tensor(shape要一致)或数字,相同位置进行比较,返回TRUE or FALSE;

           如果二者有一个为数字,则tensor的每个位置与该数字比较,返回TRUE or FALSE。

代码示例(jupyter lab)

a=[[1,2,0,0,1]]
s=tf.math.equal(a,0)
s

输出为

<tf.Tensor: shape=(1, 5), dtype=bool, numpy=array([[False, False,  True,  True, False]])>

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
非常抱歉,我的回答有误。在 TensorFlow 2.x 版本中,`tf.placeholder` 已经被移除了,取而代之的是 `tf.Variable` 和 `tf.constant`。 以下是一个使用 `tf.Variable` 和 `tf.constant` 的自动标点模型的代码: ```python import tensorflow as tf import numpy as np # 定义超参数 learning_rate = 0.01 training_epochs = 1000 display_step = 50 # 定义训练数据 train_X = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]) train_Y = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0]) # 定义输入和输出 X = tf.Variable(train_X, dtype=tf.float32) Y = tf.Variable(train_Y, dtype=tf.float32) # 定义模型参数 W = tf.Variable(np.random.randn(), name="weight", dtype=tf.float32) b = tf.Variable(np.random.randn(), name="bias", dtype=tf.float32) # 定义模型 pred = tf.sigmoid(tf.add(tf.multiply(X, W), b)) # 定义损失函数 cost = tf.reduce_mean(-Y * tf.math.log(pred) - (1 - Y) * tf.math.log(1 - pred)) # 定义优化器 optimizer = tf.optimizers.SGD(learning_rate).minimize(cost) # 开始训练模型 for epoch in range(training_epochs): optimizer.run() if (epoch + 1) % display_step == 0: c = cost.eval() print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(c), \ "W=", W.eval(), "b=", b.eval()) print("Optimization Finished!") training_cost = cost.eval() print("Training cost=", training_cost, "W=", W.eval(), "b=", b.eval(), '\n') # 测试模型 test_X = np.array([2.0, 4.0, 6.0, 8.0]) test_Y = np.array([0.0, 0.0, 1.0, 1.0]) accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(pred), Y), "float32")) print("Accuracy:", accuracy.eval({X: test_X, Y: test_Y})) ``` 该模型同样使用的是逻辑回归算法,通过输入一段文本,输出相应的标点符号。你可以根据自己的需求修改代码中的训练数据、超参数等来训练自己的自动标点模型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

同学陈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值