I am following these examples to convert my csv file to tfrecords.
This is the code I attempted
csv = pd.read_csv("ehealth.csv").values
with tf.python_io.TFRecordWriter("ehealth.tfrecords") as writer:
for row in csv:
question, answer, question_bert, answer_bert = row[0], row[1] , row[1], row[2]
example = tf.train.Example()
example.features.feature["question"].bytes_list.value.extend(question.encode("utf8"))
example.features.feature["answer"].bytes_list.value.extend(answer.encode("utf8"))
example.features.feature["question_bert"].float_list.value.extend(question_bert)
example.features.feature["answer_bert"].float_list.value.append(answer_bert)
writer.write(example.SerializeToString())
This is my error
TypeError Traceback (most recent call last) <ipython-input-36-0a8c5e073d84> in <module>()
4 question, answer, question_bert, answer_bert = row[0], row[1] , row[1], row[2]
5 example = tf.train.Example()
----> 6 example.features.feature["question"].bytes_list.value.extend(question.encode("utf8"))
7 example.features.feature["answer"].bytes_list.value.extend(answer.encode("utf8"))
8 example.features.feature["question_bert"].float_list.value.extend(question_bert)
TypeError: 104 has type int, but expected one of: bytes
It looks like there is an issue when encoding the string. I commented those two lines to make sure everything else is working correctly,
csv = pd.read_csv("ehealth.csv").values
with tf.python_io.TFRecordWriter("ehealth.tfrecords") as writer:
for row in csv:
question, answer, question_bert, answer_bert = row[0], row[1] , row[1], row[2]
example = tf.train.Example()
# example.features.feature["question"].bytes_list.value.extend(question)
# example.features.feature["answer"].bytes_list.value.extend(answer)
example.features.feature["question_bert"].float_list.value.extend(question_bert)
example.features.feature["answer_bert"].float_list.value.append(answer_bert)
writer.write(example.SerializeToString())
but then I get these errors
TypeError Traceback (most recent call last) <ipython-input-13-565b43316ef5> in <module>()
6 # example.features.feature["question"].bytes_list.value.extend(question)
7 # example.features.feature["answer"].bytes_list.value.extend(answer)
----> 8 example.features.feature["question_bert"].float_list.value.extend(question_bert)
9 example.features.feature["answer_bert"].float_list.value.append(answer_bert)
10 writer.write(example.SerializeToString())
TypeError: 's' has type str, but expected one of: int, long, float
It turns out that the issue is pandas is interpreting my array as a string instead of an array
type( csv[0][2])
->str
Furthermore, it looks like I have to use example.SerializeToString() since I have an array, but not sure how to go about doing that.
Below is the full code to reproduce the errors including code which downloads the csv file from a google drive.
import pandas as pd
import numpy as np
import requests
import tensorflow as tf
def download_file_from_google_drive(id, destination):
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params = { 'id' : id }, stream = True)
token = get_confirm_token(response)
if token:
params = { 'id' : id, 'confirm' : token }
response = session.get(URL, params = params, stream = True)
save_response_content(response, destination)
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
# download_file_from_google_drive('1rMjqKkMnt6_vROrGmlTGStNGmwPO4YFX', 'model.zip') #
file_id = '1anbEwfViu9Rzu7tWKgPb_We1EwbA4x1-'
destination = 'ehealth.csv'
download_file_from_google_drive(file_id, destination)
healthdata=pd.read_csv('ehealth.csv')
healthdata.head()
csv = pd.read_csv("ehealth.csv").values
with tf.python_io.TFRecordWriter("ehealth.tfrecords") as writer:
for row in csv:
question, answer, question_bert, answer_bert = row[0], row[1] , row[1], row[2]
example = tf.train.Example()
example.features.feature["question"].bytes_list.value.extend(question)
example.features.feature["answer"].bytes_list.value.extend(answer)
example.features.feature["question_bert"].float_list.value.extend(question_bert)
example.features.feature["answer_bert"].float_list.value.append(answer_bert)
writer.write(example.SerializeToString())
csv = pd.read_csv("ehealth.csv").values
with tf.python_io.TFRecordWriter("ehealth.tfrecords") as writer:
for row in csv:
question, answer, question_bert, answer_bert = row[0], row[1] , row[1], row[2]
example = tf.train.Example()
# example.features.feature["question"].bytes_list.value.extend(question)
# example.features.feature["answer"].bytes_list.value.extend(answer)
example.features.feature["question_bert"].float_list.value.extend(question_bert)
example.features.feature["answer_bert"].float_list.value.append(answer_bert)
writer.write(example.SerializeToString())