Prompts

Summurizing 结构 请为文章提供一个结构化的概要 理解 请介绍一下该技术的背景与发展历史。 请介绍一下主要特性与关键技术。 请列举上述介绍中涉及到的技术概念,请详细阐述涉及的每个概念,并举例说明。 (请重点解释上述概念间的差异与对比。) 使用 请介绍如何安装相关依赖或工具。 请介绍具体使用方式,并给出相应例子。 (简单介绍一下实际应用案例。) (使用中遇到哪些常见问题,如何解决。) 拓展 有没有类似工具,请对比其优劣 格式 请检查文章的格式,要求语句流畅,无错别字,格式美观,中英文间有空格,变量或命令用反括号。请直接返回校正后的文章。 Code Reading README 主要内容 依赖包,以及它们的功能 项目结构 文档、wiki Learning Summarising Summarise the above text into the most important points. Display the points as bullet points with short descriptions. Mind Map Create a mind map on the topic above. List out the central idea, main branches, and sub-branches. Active Recall I’ve recently immersed myself in studying [specific topic]. To ensure I’ve truly grasped the essence and intricacies, could you challenge my understanding by posing some deep, thought-provoking questions? This will not only test my recall of the main concepts and principles but also solidify my grasp on the finer details. ...

January 1, 2000

PyTorch

PyTorch PyTorch - QuickStart Basic # version torch.__version__ # PyTorch version torch.version.cuda # Corresponding CUDA version torch.backends.cudnn.version() # Corresponding cuDNN version torch.cuda.get_device_name(0) # GPU type # seed torch.manual_seed(0) torch.cuda.manual_seed_all(0) # GPU torch.cuda.is_available() os.environ['CUDA_VISIBLE_DEVICES'] = '0,1' torch.cuda.empty_cache() # clear GPU cache device = ( "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" ) Tensor tensor = torch.randn(2, 3) # tensor info tensor.type() # Data type tensor.size() # Shape of the tensor tensor.shape # Shape of the tensor tensor.dim() # Number of dimensions # type convertions tensor = tensor.cuda() tensor = tensor.cpu() tensor = tensor.float() tensor = tensor.long() torch.set_default_tensor_type(torch.FloatTensor) # Set default tensor type # torch.Tensor <=> np.ndarray ndarray = tensor.cpu().numpy() tensor = torch.from_numpy(ndarray).float() tensor = torch.from_numpy(ndarray.copy()).float() # If ndarray has negative stride # 从只包含一个元素的张量中提取值 value = tensor.item() # torch.Tensor <=> PIL.Image # PyTorch中的张量默认采用N×D×H×W的顺序,并且数据范围在[0, 1],需要进行转置和规范化。 image = PIL.Image.fromarray(torch.clamp(tensor * 255, min=0, max=255 ).byte().permute(1, 2, 0).cpu().numpy()) image = torchvision.transforms.functional.to_pil_image(tensor) # Equivalently way tensor = torch.from_numpy(np.asarray(PIL.Image.open(path)) ).permute(2, 0, 1).float() / 255 tensor = torchvision.transforms.functional.to_tensor(PIL.Image.open(path)) # Equivalently way # np.ndarray <=> PIL.Image image = PIL.Image.fromarray(ndarray.astypde(np.uint8)) ndarray = np.asarray(PIL.Image.open(path)) # reshape tensor.reshape(shape) tensor.permute(0,2,1) # swap axes tensor.flatten() # 展成 1D 向量 tensor.squeeze() # 去掉维数为 1 的的维度 tensor.unsqueeze(dim=0) # 增加维度 # shuffle tensor = tensor[torch.randperm(tensor.size(0))] # Shuffle the first dimension # copy # Operation | New/Shared memory | Still in computation graph | tensor.clone() # | New | Yes | tensor.detach() # | Shared | No | tensor.detach().clone() # | New | No | # concatenate tensor = torch.cat(list_of_tensors, dim=0) tensor = torch.stack(list_of_tensors, dim=0) # one-hot N = tensor.size(0) one_hot = torch.zeros(N, num_classes).long() one_hot.scatter_(dim=1, index=torch.unsqueeze(tensor, dim=1), src=torch.ones(N, num_classes).long()) # non-zero torch.nonzero(tensor) # Index of non-zero elements torch.nonzero(tensor == 0) # Index of zero elements torch.nonzero(tensor).size(0) # Number of non-zero elements torch.nonzero(tensor == 0).size(0) # Number of zero elements # equal torch.allclose(tensor1, tensor2) # float tensor torch.equal(tensor1, tensor2) # int tensor # expand # Expand tensor of shape 64*512 to shape 64*512*7*7. torch.reshape(tensor, (64, 512, 1, 1)).expand(64, 512, 7, 7) # normalize F.normalize(tensor, dim=1) Dataloader from torch.utils.data import TensorDataset, Dataset, DataLoader train_ds = TensorDataset(x_train[:50000], y_train[:50000]) valid_ds = TensorDataset(x_train[50000:], y_train[50000:]) train_dl = DataLoader(train_ds, batch_size=mini_batch, shuffle=True, num_workers=4) valid_dl = DataLoader(valid_ds, batch_size=len(valid_ds)) for xb, yb in train_dl: pass 自定义dataset ...

January 1, 2000

Reinforcement Learning

References Github - awesome-rl Github - awesome-deep-rl Github - rl-book Unity ML-Agents Toolkit Github - train_your_own_game_AI Gymnasium Github - CleanRL Frameworks Github - CleanRL Github - Stable Baselines3 Autonomous Vehicle Github - CARLA Github - CARLA-SB3-RL-Training-Environment LLM Github - trl Github - Verl Github - ROLL

January 1, 2000

sklearn-Autograd

sklearn-Autograd sklearn Prepocessing from sklearn.preprocessing import LabelEncoder, OneHotEncoder, LabelBinarizer # feature (OneHotEncoder) v.s. labels (LabelBinarizer) data = ['cold', 'cold', 'warm', 'cold', 'hot', 'hot', 'warm', 'cold', 'warm', 'hot'] LabelEncoder().fit_transform(data) # -> array([0, 0, 2, 0, 1, 1, 2, 0, 2, 1]) OneHotEncoder(sparse=False).fit_transform(np.array(data).reshape(-1,1)) # -> array([[1., 0., 0.], # [1., 0., 0.], # ..., # [0., 1., 0.]]) LabelBinarizer().fit_transform(data) # -> array([[1, 0, 0], # [1, 0, 0], # ..., # [0, 1, 0]]) # one label v.s. multilabels data = [["US", "M"], ["UK", "M"], ["FR", "F"]] OneHotEncoder(sparse=False).fit_transform(data) # -> array([[0., 0., 1., 0., 1.], # [0., 1., 0., 0., 1.], # [1., 0., 0., 1., 0.]]) MultiLabelBinarizer().fit_transform(data) # -> array([[0, 0, 1, 0, 1], # [0, 0, 1, 1, 0], # [1, 1, 0, 0, 0]]) Feature Selection sklearn - Feature selection 知乎 - 特征选择方法全面总结 Filter Methods Pearson’s Correlation ...

January 1, 2000

Tensorflow

Basic # define constant a = tf.constant(1) b = tf.constant(2, name='const', shape=(3,5), dtype=tf.float64) # define operation add = a + b mul = tf.square(tf.multiply(a, b)) # define placeholder p = tf.placeholder(tf.int32) # define variables x = tf.get_variable('x', [], dtype=tf.int32) y = tf.get_variable('y', shape=(3,5), initializer=tf.constant_initializer(0)) # session a = tf.constant(1) b = tf.constant(2) f = a + b sess = tf.Session() print(sess.run(f)) # constant do not need initialization x = tf.get_variable('x', [], dtype=tf.int32) f = x + a sess = tf.Session() sess.run(x.initializer) # variable must be initialized before run # return multiple items in a single sess.run() call, instead of making multiple calls print(sess.run([x, f])) p = tf.placeholder(tf.int32) sess = tf.Session() print(sess.run(p, feed_dict={p:2})) # placeholder must be feed in run # assignment # assign node is not connected to the variable, it change the variable's value by side effect x = tf.get_variable('x', [], dtype=tf.int32) a = tf.constant(1) assign = tf.assign(x, a) sess = tf.Session() sess.run(assign) print(sess.run(x)) # sess with block x = tf.get_variable('x', shape=(3,5), initializer=tf.constant_initializer(0)) y = tf.get_variable('y', shape=(3,5), initializer=tf.constant_initializer(0)) f = x + y with tf.Session() as sess: x.initializer.run() y.initializer.run() print(f.eval()) x = tf.get_variable('x', shape=(3,5), initializer=tf.constant_initializer(0)) y = tf.get_variable('y', shape=(3,5), initializer=tf.constant_initializer(0)) f = x * x g = f + y with tf.Session() as sess: x.initializer.run() y.initializer.run() f_val, g_val = sess.run([f, g]) # eval f and g in one run print(f_val, g_val) # interative session x = tf.get_variable('x', [], initializer=tf.constant_initializer(3)) f = x * x sess = tf.InteractiveSession() x.initializer.run() print(f.eval()) # global initializer x = tf.get_variable('x', shape=[], initializer=tf.constant_initializer(1)) y = tf.get_variable('y', shape=[], initializer=tf.constant_initializer(2)) f = x + y init = tf.global_variables_initializer() with tf.Session() as sess: init.run() print(f.eval()) # name scope x = tf.get_variable('x', [], , initializer=tf.constant_initializer(1)) with tf.variable_scope('scope'): y = tf.get_variable('x', [], , initializer=tf.constant_initializer(2)) print(x.name, y.name) # get graph default_graph = tf.get_default_graph() print(default_graph) new_graph = tf.Graph() with new_graph.as_default(): x = tf.Variable(2) print(x.graph) Save and Load a Model # save a model x = tf.get_variable('x', []) y = tf.get_variable('y', []) init = tf.global_variables_initializer() # define the saver after every variable is defined saver = tf.train.Saver() sess = tf.Session() sess.run(init) # save the model after run saver.save(sess, './models/test-model') # load a model x = tf.get_variable('x', []) y = tf.get_variable('y', []) saver = tf.train.Saver() sess = tf.Session() saver.restore(sess, './models/test-model') # no need to init sess.run([x, y]) Optimization # autodiff x = tf.get_variable('x', [], initializer=tf.constant_initializer(3.)) y = tf.get_variable('y', [], initializer=tf.constant_initializer(2.)) f = y*x + x*x + 3*x grad = tf.gradients(f, [x, y]) sess = tf.Session() sess.run(x.initializer) sess.run(y.initializer) print(sess.run(grad)) # return [df/dx, df/dy] # example k = tf.get_variable('k', [], initializer=tf.constant_initializer(0.)) b = tf.get_variable('b', [], initializer=tf.constant_initializer(0.)) init = tf.global_variables_initializer() x = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32) y_pred = k * x + b loss = tf.square(y - y_pred) optimizer = tf.train.GradientDescentOptimizer(1e-3) train_op = optimizer.minimize(loss) sess = tf.Session() sess.run(init) import random true_k = random.random() true_b = random.random() for update_i in range(10000): input_data = random.random() output_data = random.random() _loss, _ = sess.run([loss, train_op], feed_dict={x: input_data, y: output_data}) print(update_i, _loss) print('True parameter: k={}, b={}'.format(true_k, true_b)) print('True parameter: k={}, b={}'.format(sess.run([m, b]))) Tensorboard from datetime import datetime now = datetime.now().strftime("%Y%m%d%H%M%S") root_logdir = "tf_logs" logdir = "{}/run-{}/".format(root_logdir, now) # end of construction phase mse_summary = tf.summary.scalar('MSE', mse) file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph()) # execution phase for batch_index in range(n_batches): X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size) if batch_index % 10 == 0: summary_str = mse_summary.eval(feed_dict={X: X_batch, y: y_batch}) step = epoch * n_batches + batch_index file_writer.add_summary(summary_str, step) sess.run(training_op, feed_dict={X: X_batch, y: y_batch}) # finally file_writer.close() # start the Tensorboard service !tensorboard --logdir tf_logs/ Keras sequential model import keras from keras.models import Sequential from keras.layers import Dense (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() y_train = keras.utils.to_categorical(y_train) model = Sequential() model.add(Dense(100, activation='relu', input_shape=(28*28,))) model.add(Dense(100, activation='relu')) model.add(Dense(10, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc']) history = model.fit(x_train.reshape(-1, 28*28), y_train, validation_split=0.1, batch_size=32, epochs=10) general model import keras from keras.layers import Input, Dense from keras.models import Model (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() y_train = keras.utils.to_categorical(y_train) input_tensor = Input(shape=(28*28, )) x = Dense(100, activation='relu')(input_tensor) x = Dense(100, activation='relu')(x) output_tensor = Dense(10, activation='softmax')(x) model = Model(input_tensor, output_tensor) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc']) history = model.fit(x_train.reshape(-1, 28*28), y_train, validation_split=0.1, batch_size=32, epochs=10) Use Keras Layers in Tensorflow import tensorflow as tf from tensorflow import keras from tensorflow.keras.layers import Dense x = tf.placeholder(name='x', shape=(None, 28*28), dtype=tf.float32) hidden1 = Dense(100, activation='relu')(x) hidden2 = Dense(100, activation='relu')(hidden1) output = Dense(10)(hidden2) y = tf.placeholder(tf.int64, shape=(None,)) xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=output) loss = tf.reduce_mean(xentropy, name="loss") # train with tensorflow # ... Use Keras Model in Tensorflow import tensorflow as tf from tensorflow import keras from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential() model.add(Dense(100, activation='relu', input_shape=(28*28,))) model.add(Dense(10, activation='softmax')) x = tf.placeholder(name='input', shape=(None, 28*28), dtype=tf.float32) y = model(x) Check GPU # check if tensorflow is gpu version pip list | grep tensorflow from keras import backend as K K.tensorflow_backend._get_available_gpus() from tensorflow.python.client import device_lib print(device_lib.list_local_devices()) import tensorflow as tf sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) with tf.device('/gpu:0'): a = tf.constant([1., 2., 3.], shape=[1,3], name='a') b = tf.constant([1., 2., 3.], shape=[3,1], name='b') c = tf.matmul(a, b) with tf.Session() as sess: print(sess.run(c))

January 1, 2000