背景:
在使用tensorflow2.x的时候,当使用tf.linspace的时候会出现如下的报错:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from tensorflow import keras
import numpy as np
def out():
# x = tf.constant(np.arange(12).reshape(2,6))
x = tf.linspace(-6 , 6, 12)
print(x)
target = tf.sigmoid(x)
print(target)
def __test_out__():
out()
if __name__ == '__main__':
__test_out__()
解决方案:
查看官方文档:
https://www.tensorflow.org/api_docs/python/tf/linspacehttps://www.tensorflow.org/api_docs/python/tf/linspace
上面说的很清楚,start、stop必须是float类型的,
因此,代码修改如下:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from tensorflow import keras
import numpy as np
def out():
# x = tf.constant(np.arange(12).reshape(2,6))
x = tf.linspace(-6. , 6., 12)
print(x)
target = tf.sigmoid(x)
print(target)
def __test_out__():
out()
if __name__ == '__main__':
__test_out__()
正常运行!!!