tensorflow(3)---张量生成

tensorflow中的tensor就是张量,是多维数组,多维列表,用阶表示张量维度

0阶张量表示标量scalar,一个单独的数,例如123

1阶张量表示向量vector,一个一维数组,例如[1,2,3]

2阶张量叫做矩阵matrix,一个二维数组,例如[[1,2,3],[4,5,6],[7,8,9]]

n阶张量叫做张量tensor,n维数组,t=[[[[....n个]]]]

张量表示0-n阶数组

tensorflow数据类型:

tf.int                 tf.int 32          tf.float32        tf.float64    

布尔型        tf.bool            tf.constant([True,False])

字符串型        tf.string        tf.constant("Hello,world!")

1.如何创建一个tensor

import tensorflow as tf

# 创建一个张量tf.constant(张量内容,dtype=数据类型)
a = tf.constant([1,5],dtype=tf.int64)
print(a)
print(a.dtype)
print(a.shape)

输出a会输出a的所有信息,包括数据内容,形状,类型

2.numpy数组转化为Tensor

import tensorflow as tf
import numpy as np
# 将numpy数据类型转换为tensor数据类型 tf.convert_to_tensor(数据名,dtype=数据类型)
a = np.arange(0,5)
b = tf.convert_to_tensor(a)
print(a)
print(b)

 

 3.创建特殊张量

# 创建全为0的张量  tf.zeros(维度)
# 全为1的张量     tf.ones(维度)
# 创建全为指定值的张量  tf.fill(维度,指定值)

a = tf.zeros([2,3])
b = tf.ones(4)
c = tf.fill([2,2],9)
print(a)
print(b)
print(c)

 4.生成随机数

# 生成正态分布随机数,默认均值为0,标准差为1 tf.random.normal(维度,mean=均值,stddev=标准差)
# 生成截断式正态分布随机数 tf.random.truncated_normal(维度,mean=均值,stddev=标准差)
# 生成均匀分布随机数[minval,maxval] tf.random.uniform(维度,minval=最小值,maxval=最大值)
d = tf.random.normal([2,2],mean=0.5,stddev=1)
print(d)
e = tf.random.truncated_normal([2,2],mean=0.5,stddev=1)
print(e)
# 里面的元素符合以0.5为均值,1为标准差
f = tf.random.uniform([2,2],minval=0,maxval=1)
print(f)

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用TensorFlow实现K-Means算法的示例代码: ```python import numpy as np import tensorflow as tf # 生成样本数据 num_points = 200 vectors_set = [] for i in range(num_points): if np.random.random() > 0.5: vectors_set.append([np.random.normal(0.0, 0.9), np.random.normal(0.0, 0.9)]) else: vectors_set.append([np.random.normal(3.0, 0.5), np.random.normal(1.0, 0.5)]) # 将数据转换TensorFlow的常量张量 vectors = tf.constant(vectors_set) # 定义K-Means算法的簇数 k = 4 # 随机初始化质心 centroids = tf.Variable(tf.slice(tf.random.shuffle(vectors), [0, 0], [k, -1])) # 计算每个样本点到各个质心的距离 expanded_vectors = tf.expand_dims(vectors, 0) expanded_centroids = tf.expand_dims(centroids, 1) distances = tf.reduce_sum(tf.square(tf.subtract(expanded_vectors, expanded_centroids)), 2) assignments = tf.argmin(distances, 0) # 更新质心位置 means = [] for c in range(k): means.append(tf.reduce_mean(tf.gather(vectors, tf.reshape(tf.where(tf.equal(assignments, c)), [1, -1])), reduction_indices=[1])) new_centroids = tf.concat(means, 0) # 初始化变量并运行会话 init_op = tf.global_variables_initializer() sess = tf.Session() sess.run(init_op) # 执行K-Means算法 for step in range(100): _, centroid_values, assignment_values = sess.run([new_centroids, centroids, assignments]) if step % 10 == 0: print("Step {}: Centroids = {}".format(step, centroid_values)) # 关闭会话 sess.close() ``` 这段代码生成了200个二维数据点,并使用K-Means算法将它们分为4个簇。在每一步迭代中,算法会计算每个数据点到各个质心的距离并将每个点分配到距离最近的簇中。然后,算法会根据当前的簇分配来更新质心的位置,并重复以上步骤直到收敛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值