我是python的新手.这似乎是一个要问的基本问题.但我真的想了解这里发生的事情
import numpy as np
import pandas as pd
tempdata = np.random.random(5)
myseries_one = pd.Series(tempdata)
myseries_two = pd.Series(data = tempdata, index = ['a','b','c','d','e'])
myseries_three = pd.Series(data = tempdata, index = [10,11,12,13,14])
myseries_one
Out[1]:
0 0.291293
1 0.381014
2 0.923360
3 0.271671
4 0.605989
dtype: float64
myseries_two
Out[2]:
a 0.291293
b 0.381014
c 0.923360
d 0.271671
e 0.605989
dtype: float64
myseries_three
Out[3]:
10 0.291293
11 0.381014
12 0.923360
13 0.271671
14 0.605989
dtype: float64
Run Code Online (Sandbox Code Playgroud)
索引每个数据帧的第一个元素
myseries_one[0] #As expected
Out[74]: 0.29129291112626043
myseries_two[0] …Run Code Online (Sandbox Code Playgroud) 我正在使用并发.futures 模块来进行多处理和多线程处理。我在具有 16GB RAM、英特尔 i7 第八代处理器的 8 核机器上运行它。我在 Python 3.7.2 甚至 Python 3.8.2 上尝试过这个
import concurrent.futures
import time
Run Code Online (Sandbox Code Playgroud)
获取列表并将每个元素乘以 2
def double_value(x):
y = []
for elem in x:
y.append(2 *elem)
return y
Run Code Online (Sandbox Code Playgroud)
将 elem 乘以 2
def double_single_value(x):
return 2* x
Run Code Online (Sandbox Code Playgroud)
定义一个
import numpy as np
a = np.arange(100000000).reshape(100, 1000000)
Run Code Online (Sandbox Code Playgroud)
运行多个线程并将每个 elem 乘以 2 的函数
def get_double_value(x):
with concurrent.futures.ThreadPoolExecutor() as executor:
results = executor.map(double_single_value, x)
return list(results)
Run Code Online (Sandbox Code Playgroud)
t = time.time()
with concurrent.futures.ProcessPoolExecutor() as executor:
my_results = …Run Code Online (Sandbox Code Playgroud) I have trained a TensorFlow model and convert it to TensorFlow lite using the below code:
# Convert the model
import tensorflow as tf
import numpy as np
# path to the SavedModel directory is TFLITE_PATH
converter = tf.lite.TFLiteConverter.from_saved_model(TFLITE_PATH)
tflite_model = converter.convert()
# Save the model.
with open('model_1.tflite', 'wb') as f:
f.write(tflite_model)
Run Code Online (Sandbox Code Playgroud)
Attaching my model_1.tflite model in case you want to investigate. I have tested it inside my python environment, where it is producing output using the below script:
import …Run Code Online (Sandbox Code Playgroud) 我创建了一个 tf 记录并用它来训练我的模型。在训练过程中,每当我调用数据来执行评估时,我总是收到消息
GeneratorDatasetOp::Dataset 的输入不会被优化,因为数据集没有实现应用优化所需的 AsGraphDefInternal() 方法。
在创建 tf 记录期间,我尝试包含以下代码来优化数据集,但这没有成功
AUTO = tf.data.experimental.AUTOTUNE # used in tf.data.Dataset API
option_no_order = tf.data.Options()
option_no_order.experimental_deterministic = False
option_no_order.experimental_optimization.noop_elimination = True
option_no_order.experimental_optimization.apply_default_optimizations = True
Run Code Online (Sandbox Code Playgroud)
请建议我可以尝试或遵循的优化数据集的步骤
我创建了从00:00:00到23:00:00的时间序列.在总和小于或等于23:00:00之前,添加任意小时数都很容易.在总和超过23:00:00后,它开始显示天数的时间,这是非常直观的.但我希望输出在一个时钟时间内,无论我是减法还是加法
假设我想像下面这样做
library(chron)
times("23:00:00")+ times("01:00:00")
Time in days:
[1] 1
Run Code Online (Sandbox Code Playgroud)
我想要的输出低于一.而不是得到我想要的日子
00:00:00
Run Code Online (Sandbox Code Playgroud)
我也试过减法
times("00:00:00")- times("01:00:00")
[1] -0.04166667
Run Code Online (Sandbox Code Playgroud)
期望的输出
"23:00:00"
Run Code Online (Sandbox Code Playgroud)
我也尝试使用POSIXct,但它在各种情况下都会出现各种错误
as.POSIXct("00:00:00", format = "%H:%M:%S", tz = "UTC")
[1] "2017-02-07 UTC" #Not printing time. Only dates
Run Code Online (Sandbox Code Playgroud)
使用POSIXct减法
as.POSIXct("00:00:00", format = "%H:%M:%S", tz = "UTC") -as.POSIXct("01:00:00", format = "%H:%M:%S", tz = "UTC")
Time difference of -1 hours
Warning message:
In 1:0:0 : numerical expression has 2 elements: only the first used
Run Code Online (Sandbox Code Playgroud)
使用POSIXct添加
as.POSIXct("23:00:00", format = …Run Code Online (Sandbox Code Playgroud) 我想要使用Bokeh 进行如下所示的类似可视化。由于我是Bokeh 的新手,我可能想知道是否有任何代码像下面使用Seaborn 的代码一样简洁?
我的主要重点是如何在 Bokeh 中为相同的可视化编写代码
data = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv',index_col = 0)
Run Code Online (Sandbox Code Playgroud)
import seaborn as sns
sns.pairplot(data, x_vars = ['TV', 'Radio','Newspaper'], y_vars = ['Sales'],
size =7, aspect =.7, kind = 'reg')
Run Code Online (Sandbox Code Playgroud)
此外,Seaborn 中的代码不需要输入最佳拟合线。它会自动生成图中带有置信区间阴影的最佳拟合线。在 Bokeh 中可以进行这种绘图吗?
我正在运行 2000 个时代的多类模型(总共 40 个类)。该模型运行良好,直到 828 epoch 但在 829 epoch 它给了我一个 InvalidArgumentError (见下面的截图)
下面是我用来构建模型的代码。
n_cats = 40
input_bow = tf.keras.Input(shape=(40), name="bow")
hidden_1 = tf.keras.layers.Dense(200, activation="relu")(input_bow)
hidden_2 = tf.keras.layers.Dense(100, activation="relu")(hidden_1)
hidden_3 = tf.keras.layers.Dense(80, activation="relu")(hidden_2)
hidden_4 = tf.keras.layers.Dense(70, activation="relu")(hidden_3)
output = tf.keras.layers.Dense(n_cats, activation="sigmoid")(hidden_4)
model = tf.keras.Model(inputs=[input_bow], outputs=output)
METRICS = [
tf.keras.metrics.Accuracy(name="Accuracy"),
tf.keras.metrics.Precision(name="precision"),
tf.keras.metrics.Recall(name="recall"),
tf.keras.metrics.AUC(name="auc"),
tf.keras.metrics.BinaryAccuracy(name="binaryAcc")
]
checkpoint_cb = tf.keras.callbacks.ModelCheckpoint(
"my_keras_model.h5", save_best_only=True)
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate=1e-2,
decay_steps=10000,
decay_rate=0.9)
adam_optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
model.compile(loss="categorical_crossentropy",
optimizer="adam", metrics=METRICS)
training_history = model.fit(
(bow_train),
indus_cat_train,
epochs=2000,
batch_size=128,
callbacks=[checkpoint_cb],
validation_data=(bow_test, indus_cat_test)) …Run Code Online (Sandbox Code Playgroud) 我正在使用预训练模型来训练图像分类器。下面的代码在 CPU 和单个 GPU 上运行良好(即当 #GPU=1 时)
class Metrics(tf.keras.callbacks.Callback):
def __init__(self, train_tf_data, val_tf_data, CLASSES, logs={}, **kwargs):
super().__init__(**kwargs)
# self.keras_metric = tf.keras.metrics.Mean("val_f1_after_epoch")
self.train_tf_data = train_tf_data
self.val_tf_data = val_tf_data
# self.model = model
self.CLASSES = CLASSES
def on_epoch_end(self, epoch, logs={}):
# self.keras_metric.reset_state()
# for train data
self.train_reports = test_model(model=self.model, data=self.train_tf_data, CLASSES=self.CLASSES)
self.train_f1_after_epoch = self.train_reports['f1_score']
self.train_recall_after_epoch = self.train_reports['recall']
self.train_prec_after_epoch = self.train_reports['precision']
# for val data
self.val_reports = test_model(model=self.model, data=self.val_tf_data, CLASSES=self.CLASSES)
self.val_f1_after_epoch = self.val_reports['f1_score']
self.val_recall_after_epoch = self.val_reports['recall']
self.val_prec_after_epoch = self.val_reports['precision']
# saving train results …Run Code Online (Sandbox Code Playgroud) 我正在使用 tensorflow 版本 2.0.0-beta1。打电话时
tf.estimator.inputs.pandas_input_fn
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误。
module 'tensorflow_estimator.python.estimator.api._v2.estimator' has no attribute 'inputs'
Run Code Online (Sandbox Code Playgroud)
为了找出我尝试过的问题
tf.estimator.inputs
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误
module 'tensorflow_estimator.python.estimator.api._v2.estimator' has no attribute 'inputs'
Run Code Online (Sandbox Code Playgroud)
我曾尝试在本地机器和 Google Colab 上重新安装 tensorflow,但这似乎不起作用。与先前版本的 tensorflow ie(tf 版本 1.x)相比,新版本的 tensorflow 中的此功能是否有任何变化
我的项目有以下结构
Object_Detection/
setup.py
setup.cfg
requirement.txt
object_detection/
models
__init__.py #contains from . import models
tests/
# inside tests dir
test_utils_image.py
__init__.py #empty
utils/
# inside utils dir
__init__.py #inside
utils_image_preprocess.py
utils_image.py
utils_tfrecord.py
Run Code Online (Sandbox Code Playgroud)
现在utils 目录中的init .py 包含以下代码。
# inside __init__.py
from . import utils_image_preprocess
from . import utils_image
from . import utils_tfrecord
Run Code Online (Sandbox Code Playgroud)
在init .py 文件之上运行会出现错误:
ImportError: attempted relative import with no known parent package
Run Code Online (Sandbox Code Playgroud)
测试目录中的 test_utils.py 包含以下代码
# inside test_utils.py
from object_detection.utils import utils_image
Run Code Online (Sandbox Code Playgroud)
运行 test_utils.py 时出现以下错误
ImportError: cannot import name …Run Code Online (Sandbox Code Playgroud)