【Tensorflow】numpy np.select()

np.select(condlist, choicelist, default=0)

condlist:条件列表,元素是bool数组
choicelist:列表,数组元素
default:当所有条件都不满足时,用default值填上。

condlist, choicelist的长度必须一致,每一个条件都要进行一次元素选择或者操作。

举例:

arr = np.arange(9).reshape(3,3)
conds = [arr < 3, 6 < arr]
np.select(condlist=conds, choicelist=[arr, arr*2])
array([[ 0,  1,  2],
       [ 0,  0,  0],
       [ 0, 14, 16]])
  • 通俗讲,condlist是两个bool mask,choicelist是与bool mask形状相同的数组。正常情况下,condlist中所有的数组True与False应该是互补的,共同组成形状。但是,也可以重复,这时就以第一个True条件为准,如下:
conds = [arr < 8, 6 < arr]
np.select(condlist=conds, choicelist=[arr, arr*2])
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7, 16]])
  • condlist和choicelist中出现的数组可以不是同一个数组,但是。他们的形状必须形同。
arr = np.arange(9).reshape(3,3)
arr2 = np.arange(10,19).reshape(3,3)
conds = [arr < 3, 6 < arr]
np.select(condlist=conds, choicelist=[arr2, arr2*2])
array([[10, 11, 12],
       [ 0,  0,  0],
       [ 0, 34, 36]])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于Python和tensorflow框架实现的遗传算法优化的多层感知机网络做回归预测的示例代码: ```python import tensorflow as tf import numpy as np import random # 定义神经网络的参数 n_input = 2 # 输入层的节点数 n_hidden = 5 # 隐藏层的节点数 n_output = 1 # 输出层的节点数 # 定义遗传算法的参数 pop_size = 50 # 种群大小 elite_size = 5 # 精英个体的数量 mutation_rate = 0.01 # 变异率 # 定义适应度函数,即预测误差的平方和 def fitness(individual, x_data, y_data): # 将个体的基因转换为神经网络的权重和偏置 w1 = np.array(individual[:n_input*n_hidden]).reshape((n_input,n_hidden)) b1 = np.array(individual[n_input*n_hidden:n_input*n_hidden+n_hidden]).reshape((n_hidden,)) w2 = np.array(individual[n_input*n_hidden+n_hidden:n_input*n_hidden+n_hidden+n_hidden*n_output]).reshape((n_hidden,n_output)) b2 = np.array(individual[n_input*n_hidden+n_hidden+n_hidden*n_output:]).reshape((n_output,)) # 使用转换后的权重和偏置构建神经网络 hidden = tf.nn.relu(tf.matmul(x_data, w1) + b1) output = tf.matmul(hidden, w2) + b2 # 计算预测误差的平方和 loss = tf.reduce_mean(tf.square(output - y_data)) # 返回预测误差的平方和的倒数,因为遗传算法是求最小值,而我们需要最小化预测误差的平方和 return 1.0 / (1.0 + loss) # 定义遗传算法的操作 def select(population, fitness_values): # 使用轮盘赌选择算法选择下一代个体 total_fitness = np.sum(fitness_values) probabilities = fitness_values / total_fitness selected_indices = np.random.choice(np.arange(pop_size), size=pop_size, p=probabilities, replace=True) selected_population = [population[i] for i in selected_indices] # 从选择的个体中随机选择精英个体 elite_indices = np.random.choice(np.arange(pop_size), size=elite_size, replace=False) elite_population = [selected_population[i] for i in elite_indices] # 返回精英个体和选择的个体 return elite_population, selected_population def crossover(parent1, parent2): # 使用单点交叉算法实现交叉操作 crossover_point = random.randint(0, len(parent1)) child1 = parent1[:crossover_point] + parent2[crossover_point:] child2 = parent2[:crossover_point] + parent1[crossover_point:] # 返回两个子代 return child1, child2 def mutate(individual): # 使用随机突变算法实现变异操作 for i in range(len(individual)): if random.random() < mutation_rate: individual[i] = random.uniform(-1.0, 1.0) # 返回变异后的个体 return individual # 生成初始种群 population = [np.random.uniform(-1.0, 1.0, size=(n_input*n_hidden+n_hidden+n_hidden*n_output+n_output)) for i in range(pop_size)] # 加载数据 x_data = np.random.uniform(-1.0, 1.0, size=(100, n_input)) y_data = np.sin(x_data[:,0]*x_data[:,1]*np.pi) # 迭代遗传算法 for generation in range(100): # 计算适应度 fitness_values = [fitness(individual, x_data, y_data) for individual in population] # 选择下一代个体 elite_population, selected_population = select(population, fitness_values) # 交叉和变异产生新的个体 num_children = pop_size - elite_size children = [] for i in range(num_children // 2): parent1 = random.choice(selected_population) parent2 = random.choice(selected_population) child1, child2 = crossover(parent1, parent2) children.append(mutate(child1)) children.append(mutate(child2)) if num_children % 2 == 1: parent1 = random.choice(selected_population) parent2 = random.choice(selected_population) child1, child2 = crossover(parent1, parent2) children.append(mutate(child1)) # 拼接精英个体和新个体得到下一代种群 population = elite_population + children # 输出当前最优个体的适应度 best_individual = elite_population[0] best_fitness = fitness_values[population.index(best_individual)] print("Generation {}: best fitness = {:.6f}".format(generation+1, best_fitness)) ``` 在这个示例代码中,我们使用一个包含2个输入节点、5个隐藏节点和1个输出节点的多层感知机网络对一组随机生成的数据进行回归预测。我们使用遗传算法来优化神经网络的权重和偏置,其中适应度函数是预测误差的平方和的倒数。在遗传算法的操作中,我们使用轮盘赌选择算法选择下一代个体,使用单点交叉算法实现交叉操作,使用随机突变算法实现变异操作。在迭代过程中,我们输出当前最优个体的适应度,以便查看遗传算法是否能够找到较好的解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值