wuxiang commented on issue zeizei/OpenI_Learning#7
云脑npu无法正常工作问题# 矩阵乘法: ``` # ms import os,time import numpy as np import urllib.request from urllib.parse import urlparse import gzip import argparse import mindspore as ms import mindspore.dataset as ds import mindspore.nn as nn from mindspore import context from mindspore.train.serialization import load_checkpoint, load_param_into_net from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor from mindspore.train import Model from mindspore.common.initializer import TruncatedNormal import mindspore.dataset.vision.c_transforms as CV # import mindspore.dataset.transforms.vision.c_transforms as CV import mindspore.dataset.vision.c_transforms as C from mindspore.nn.metrics import Loss from mindspore.common import dtype as mstype from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits, MSELoss from mindspore import Tensor from mindspore import dtype as mstype def fc_with_initialize(input_channels, out_channels): """Fc layer weight initial.""" weight = weight_variable() bias = weight_variable() return nn.Dense(input_channels, out_channels, weight, bias) def weight_variable(): """Weight initial.""" return TruncatedNormal(0.02) a = np.random.rand(512,4096*4).astype(np.float32) a = Tensor(a) fc = fc_with_initialize(4096*4, 4096*4) b = fc(a) b[:10, :10] ```
2 years ago
wuxiang commented on issue zeizei/OpenI_Learning#7
云脑npu无法正常工作问题``` from npu_bridge.npu_init import * import time import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf import numpy as np import time n_input = 463 n_hidden = 20000 n_output = 463 data_train = np.random.rand(512*100, n_input) data_train = tf.data.Dataset.from_tensor_slices((data_train, data_train)).batch(512) x =tf.placeholder('float32', shape=[None,n_input]) # 特征 y_ = tf.placeholder('float32', shape=[None,n_input]) # 特征 w1, b1 = tf.Variable(tf.random_normal([n_input,n_hidden])), tf.Variable(tf.random_normal([n_hidden])) w2, b2 = tf.Variable(tf.random_normal([n_hidden,n_output])), tf.Variable(tf.random_normal([n_output])) fc1_result = tf.nn.relu(tf.matmul(x, w1) + b1) fc2_result = tf.nn.relu(tf.matmul(fc1_result, w2) + b2) loss = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.pow(y_ - fc2_result, 2), axis=1)), axis=0) optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss) # optimizer = npu_tf_optimizer(optimizer).minimize(loss) init = tf.global_variables_initializer() use_npu = 1 if use_npu: pass config = tf.ConfigProto() custom_op = config.graph_options.rewrite_options.custom_optimizers.add() custom_op.name = "NpuOptimizer" custom_op.parameter_map["use_off_line"].b = True #在昇腾AI处理器执行训练 custom_op.parameter_map["mix_compile_mode"].b = False #关闭混合计算,根据实际情况配置,默认关闭 custom_op.parameter_map["enable_data_pre_proc"].b = True # getnext算子下沉是迭代循环下沉的必要条件 custom_op.parameter_map["iterations_per_loop"].i = 10 #此处设置的值和set_iteration_per_loop设置的iterations_per_loop值保持一致,用于判断是否进行训练迭代下沉 config.graph_options.rewrite_options.remapping = RewriterConfig.OFF config.graph_options.rewrite_options.memory_optimization = RewriterConfig.OFF with tf.Session(config=config) as sess: # with tf.Session() as sess: sess.run(init) start = time.time() print('start training...') train_op = util.set_iteration_per_loop(sess, optimizer, 10) iterator = data_train.make_initializable_iterator() for i in range(10): sess.run(iterator.initializer) get_next = iterator.get_next() while True: try: xx, yy = sess.run(get_next) _, tmploss = sess.run([train_op, loss], feed_dict = {x: xx, y_:yy}) # print(tmploss, end='\t') except: break # print() end = time.time() print('time use:', end-start) ```
2 years ago
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》