Python处理时间的神器-----Pendulum

本文介绍了Python中用于时间处理的强大库Pendulum,包括获取当前、昨天、今天和明天时间,时间差计算,字符串转换,时间类型测试,时间解析等功能。通过示例展示了如何使用Pendulum进行各种时间操作,如获取日期、小时、分钟和秒等信息。
摘要由CSDN通过智能技术生成

Python处理时间的神器-----Pendulum

import pendulum

if __name__ == '__main__':
    # 1-时间获取
    a1 = pendulum.now()# 现在的时间
    # 2022-03-10T13:49:23.695583+08:00
    a2 = pendulum.yesterday()# 昨天的时间
    # 2022-03-09T00:00:00+08:00 
    a3 = pendulum.today() # 今天的时间
    # 2022-03-10T00:00:00+08:00 
    a4 = pendulum.tomorrow() # 明天的时间
    # 2022-03-11T00:00:00+08:00
   
    print(a3.diff(a2).in_days()) # 计算相差多少天
    #1
    print(a3.diff(a2).in_hours()) # 计算相差多少小时
    #24
    # 2-转换字符串
    a4 = pendulum.now().to_datetime_string()
    # 2022-03-10 13:49:23
    a5 = pendulum.now().to_date_string()
    # 2022-03-10
    a6 = pendulum.now().to_time_string()
    # 13:49:23
    # 3-类型测试
    from datetime import datetime
    da = pendulum.datetime(2022, 3, 10)
    # True 

    # 4- 解析规范时间
    # pendulum.from_format(date, "YYYY-MM-DD")
    a7 = pendulum.from_format('2022-03-10', 'YYYY-MM-DD')
    # 2022-03-10T00:00:00+00:00
    a8 = pendulum.parse('2022-03-10')
    # 2022-03-10T00:00:00+00:00
    # 属性
    a9 = pendulum.now()
    print(a9.year)
    #2022
    print(a9.month)
    # 3
    print(a9.day)
    # 10
    print(a9.hour)
    # 13
    print(a9.minute)
    # 49
    print(a9.second)
    # 23

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GCN-DDPG是一种基于图卷积神经网络和深度确定性策略梯度(DDPG)的强化学习算法。以下是一个简单的Python代码实现,仅供参考: ```python import tensorflow as tf import numpy as np import gym import random from collections import deque # 定义超参数 EPISODES = 5000 BATCH_SIZE = 64 GAMMA = 0.99 TAU = 0.001 LR_ACTOR = 0.0001 LR_CRITIC = 0.001 # 定义图卷积神经网络层 class GraphConvolution(tf.keras.layers.Layer): def __init__(self, output_dim): super(GraphConvolution, self).__init__() self.output_dim = output_dim def build(self, input_shape): self.kernel = self.add_weight(name='kernel', shape=(input_shape[1], self.output_dim), initializer='glorot_uniform', trainable=True) def call(self, inputs): features, adj = inputs output = tf.matmul(adj, features) output = tf.matmul(output, self.kernel) return tf.nn.relu(output) # 定义Actor模型 class Actor(tf.keras.Model): def __init__(self, state_shape, action_shape): super(Actor, self).__init__() self.fc1 = tf.keras.layers.Dense(64, activation='relu') self.fc2 = tf.keras.layers.Dense(64, activation='relu') self.fc3 = tf.keras.layers.Dense(action_shape[0], activation='tanh') def call(self, state): x = self.fc1(state) x = self.fc2(x) x = self.fc3(x) return x # 定义Critic模型 class Critic(tf.keras.Model): def __init__(self, state_shape, action_shape): super(Critic, self).__init__() self.fc1 = tf.keras.layers.Dense(64, activation='relu') self.fc2 = tf.keras.layers.Dense(64, activation='relu') self.fc3 = tf.keras.layers.Dense(action_shape[0], activation='linear') def call(self, inputs): state, action = inputs x = tf.concat([state, action], axis=-1) x = self.fc1(x) x = self.fc2(x) x = self.fc3(x) return x # 定义Replay Buffer class ReplayBuffer: def __init__(self, buffer_size): self.buffer_size = buffer_size self.buffer = deque(maxlen=buffer_size) def add(self, state, action, reward, next_state, done): experience = (state, action, reward, next_state, done) self.buffer.append(experience) def sample(self, batch_size): batch = random.sample(self.buffer, batch_size) state_batch = np.array([experience[0] for experience in batch]) action_batch = np.array([experience[1] for experience in batch]) reward_batch = np.array([experience[2] for experience in batch]) next_state_batch = np.array([experience[3] for experience in batch]) done_batch = np.array([experience[4] for experience in batch]) return state_batch, action_batch, reward_batch, next_state_batch, done_batch def size(self): return len(self.buffer) # 定义环境 env = gym.make('Pendulum-v0') state_shape = env.observation_space.shape action_shape = env.action_space.shape # 初始化Actor和Critic模型 actor = Actor(state_shape, action_shape) critic = Critic(state_shape, action_shape) actor_target = Actor(state_shape, action_shape) critic_target = Critic(state_shape, action_shape) actor_optimizer = tf.keras.optimizers.Adam(learning_rate=LR_ACTOR) critic_optimizer = tf.keras.optimizers.Adam(learning_rate=LR_CRITIC) # 将Actor和Critic的参数复制到对应的目标网络 actor_target.set_weights(actor.get_weights()) critic_target.set_weights(critic.get_weights()) # 定义Replay Buffer replay_buffer = ReplayBuffer(10000) # 定义训练函数 @tf.function def train_actor(state): with tf.GradientTape() as tape: action = actor(state) q_value = critic([state, action]) loss = -tf.reduce_mean(q_value) gradients = tape.gradient(loss, actor.trainable_variables) actor_optimizer.apply_gradients(zip(gradients, actor.trainable_variables)) @tf.function def train_critic(state, action, reward, next_state, done): with tf.GradientTape() as tape: target_action = actor_target(next_state) target_q_value = critic_target([next_state, target_action]) y = reward + (1 - done) * GAMMA * target_q_value q_value = critic([state, action]) td_error = y - q_value loss = tf.reduce_mean(tf.square(td_error)) gradients = tape.gradient(loss, critic.trainable_variables) critic_optimizer.apply_gradients(zip(gradients, critic.trainable_variables)) # 开始训练 for episode in range(EPISODES): state = env.reset() episode_reward = 0 while True: action = actor(np.expand_dims(state, axis=0))[0] action += np.random.normal(0, 0.1, size=action_shape[0]) action = np.clip(action, -1.0, 1.0) next_state, reward, done, _ = env.step(action) replay_buffer.add(state, action, reward, next_state, done) episode_reward += reward if replay_buffer.size() >= BATCH_SIZE: state_batch, action_batch, reward_batch, next_state_batch, done_batch = replay_buffer.sample(BATCH_SIZE) train_critic(state_batch, action_batch, reward_batch, next_state_batch, done_batch) train_actor(state_batch) # 软更新Actor和Critic的目标网络 for t, e in zip(actor_target.trainable_variables, actor.trainable_variables): t.assign(t * (1 - TAU) + e * TAU) for t, e in zip(critic_target.trainable_variables, critic.trainable_variables): t.assign(t * (1 - TAU) + e * TAU) state = next_state if done: break print('Episode: {}, Reward: {}'.format(episode, episode_reward)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值