How do I define the split_train_test in Python? - python-3.8

I am working on a python code to generate network traffic using GANs and I'm getting this error that split_train_test is not defined. I have imported train_test_split from sklearn.model_selection but it doesnt seem to work. What am I not doing right?
This is the error message;
NameError Traceback (most recent call last)
<ipython-input-153-a2836ba27bc4> in <module>
9 cross_validation_flg = False
10 benign_file = '../data/attack_normal_data/benign_data.csv'
---> 11 benign_model, benign_test_loader = run_main(benign_file, num_features=41)
12 # Save the model checkpoint
13 torch.save(benign_model.state_dict(), 'benign_model_epoches%d.ckpt' % num_epochs)
<ipython-input-147-e59bfccfe2c7> in run_main(input_file, num_features)
5 dataset = TrafficDataset(input_file, transform=None, normalization_flg=True)
6
----> 7 train_sampler, test_sampler = split_train_test(dataset, split_percent=0.7, shuffle=True)
8 cntr = Counter(dataset.y)
9 print('dataset: ', len(dataset), ' y:', sorted(cntr.items()))
NameError: name 'split_train_test' is not defined

It should be train_test_split
Reference the docs

Related

Unable to find MSE using Tensorflow time series model. Error: 'mean_absolute_error' is not in list

While performing time series weather prediction from tensorflow taken from here https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/structured_data/time_series.ipynb#scrollTo=EN4U1fcMiTYs, while using this code snippet to find the performance in terms of MSE:
x = np.arange(len(performance))
width = 0.3
metric_name = 'mean_absolute_error'
metric_index = lstm_model.metrics_names.index('mean_absolute_error')
I get an error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-80-462945e7d3dd> in <module>
2 width = 0.3
3 metric_name = 'mean_absolute_error'
----> 4 metric_index = lstm_model.metrics_names.index('mean_absolute_error')
5 val_mae = [v[metric_index] for v in val_performance.values()]
6 test_mae = [v[metric_index] for v in performance.values()]
ValueError: 'mean_absolute_error' is not in list
How do I fix this?

AttributeError: 'ArrayView' object has no attribute 'A1'

I have to import a processed h5ad file, but it seems that X has been passed as a numpy array instead of a numpy matrix. See below:
# Read the data
data_path = "/home/bbb5130/snOMICS/maria/msrna.h5ad"
adata = sn.pp.read_h5ad(data_path, pr_process="Yes")
adata
But the output was:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In [15], line 3
1 # Read the data
2 data_path = "/home/bbb5130/snOMICS/maria/msrna.h5ad"
----> 3 adata = sn.pp.read_h5ad(data_path, pr_process="Yes")
4 adata
File ~/miniconda3/envs/snOMICS/lib/python3.9/site-packages/scanet/preprocessing.py:54, in Preprocessing.read_h5ad(cls, filename, pr_process)
51 return sc.read_h5ad(filename)
52 else:
53 # initial preprocessing as it is required later
---> 54 return cls._intial(adata)
File ~/miniconda3/envs/snOMICS/lib/python3.9/site-packages/scanet/preprocessing.py:35, in Preprocessing._intial(adata)
33 adata.var['mt'] = adata.var_names.str.startswith('MT-')
34 mito_genes = adata.var_names.str.startswith('MT-')
---> 35 adata.obs['percent_mito'] = np.sum(adata[:, mito_genes].X, axis=1).A1 / np.sum(adata.X, axis=1).A1
36 sc.pp.calculate_qc_metrics(adata, qc_vars=['mt'], percent_top=None, inplace=True)
37 sc.pp.filter_cells(adata, min_genes=0)
AttributeError: 'ArrayView' object has no attribute 'A1'
Is there anyway I can change the format, so the file can be read?
Thanks in advance.

AttributeError: 'list' object has no attribute 'sents'

How can I resolve this attribute error in spaCy?
from __future__ import unicode_literals, print_function
from spacy.lang.en import English
nlp = English()
sentencizer = nlp.create_pipe("sentencizer")
nlp.add_pipe(sentencizer)
assert len(list(doc.sents)) == 2
This is the traceback:
AttributeError Traceback (most recent call last)
<ipython-input-81-0459326012bf> in <module>
5 sentencizer = nlp.create_pipe("sentencizer")
6 nlp.add_pipe(sentencizer)
----> 7 assert len(list(doc.sents)) == 2
AttributeError: 'list' object has no attribute 'sents'
If your goal is to tokenize (split) sentences, below is a code sample using spaCy.
import spacy
nlp = spacy.load('en_core_web_lg')
raw_text = 'Hello, world. Here are two sentences.'
doc = nlp(raw_text)
sentences = [sent.string.strip() for sent in doc.sents]
assert len(sentences) == 2
print(sentences)
Output:
['Hello, world.', 'Here are two sentences.']

Problem with running object_detection_tutorial TypeError: load() missing 2 required positional arguments

I'm pretty new to tensorflow and I'm trying to run object_detection_tutorial. I'm getting TypeErrror and don't know how to fix it.
This is load_model function which misses 2 arguments:
tags: Set of string tags to identify the required MetaGraphDef. These should correspond to the tags used when saving the variables using the SavedModel save() API.
export_dir: Directory in which the SavedModel protocol buffer and variables to be loaded are located.
def load_model(model_name):
base_url = 'http://download.tensorflow.org/models/object_detection/'
model_file = model_name + '.tar.gz'
model_dir = tf.keras.utils.get_file(
fname=model_name,
origin=base_url + model_file,
untar=True)
model_dir = pathlib.Path(model_dir)/"saved_model"
model = tf.saved_model.load(str(model_dir))
model = model.signatures['serving_default']
return model
WARNING:tensorflow:From <ipython-input-9-f8a3c92a04a4>:11: load (from tensorflow.python.saved_model.loader_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.loader.load or tf.compat.v1.saved_model.load. There will be a new function for importing SavedModels in Tensorflow 2.0.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-e10c73a22cc9> in <module>
1 model_name = 'ssd_mobilenet_v1_coco_2017_11_17'
----> 2 detection_model = load_model(model_name)
<ipython-input-9-f8a3c92a04a4> in load_model(model_name)
9 model_dir = pathlib.Path(model_dir)/"saved_model"
10
---> 11 model = tf.saved_model.load(str(model_dir))
12 model = model.signatures['serving_default']
13
~/.local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
322 'in a future version' if date is None else ('after %s' % date),
323 instructions)
--> 324 return func(*args, **kwargs)
325 return tf_decorator.make_decorator(
326 func, new_func, 'deprecated',
TypeError: load() missing 2 required positional arguments: 'tags' and 'export_dir'
Can you help me fix this and run my first object detector :D?
I had the same problem and i'm trying to solve this for 1 week now. I guess the solution should be this;
model = tf.compat.v2.saved_model.load(str(model_dir), None)
More detail would be (from the official website) ;
Load a SavedModel from export_dir.
tf.saved_model.load(
export_dir,
tags=None
)
Aliases:
tf.compat.v1.saved_model.load_v2
tf.compat.v2.saved_model.load
I guessed it was a branch problem and using the tf_2_1_reference branch did the trick for me:
igian#iGians-MBP models % git checkout tf_2_1_reference
M research/object_detection/object_detection_tutorial.ipynb
Branch 'tf_2_1_reference' set up to track remote branch 'tf_2_1_reference' from 'origin'.
Switched to a new branch 'tf_2_1_reference'
igians#iGians-MBP models % jupyter notebook
Then executed each jupiter cell of the tutorial like a good newbie!
This is the branch i used: https://github.com/tensorflow/models/tree/tf_2_1_reference
If you would just like to make a perdiction then you can also use load the model as below:
from tensorflow.contrib import predictor
predict_fn = predictor.from_saved_model(model_dir)

tf.contrib.learn yields error message "module has no attribute 'learn' "

Here is a snippet of my code taken directly from the tf.contrib.learn tutorial on tensorflow.org:
# Load Data Sets
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename = IRIS_TRAINING,
target_dtype = np.int,
features_dtype = np.float32)
Here is the error message:
AttributeError Traceback (most recent call last)
<ipython-input-14-7122d1244c55> in <module>()
11
12 # Load Data Sets
---> 13 training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
14 filename = IRIS_TRAINING,
15 target_dtype = np.int,
AttributeError: 'module' object has no attribute 'learn'
Clearly the module has the attribute learn since tensorflow has a section on learning tf.contrib.learn. What am I doing wrong? All guidance is appreciated.